07、一键下电影

 
    用户输入喜欢的电影名字,程序即可在电影天堂爬取电影所对应的下载链接,并将下载链接打印出来。
 
 
 1 #7、一键下电影
 2 #    用户输入喜欢的电影名字,程序即可在电影天堂爬取电影所对应的下载链接,并将下载链接打印出来。
 3 #    URL https://www.ygdy8.com
 4 
 5 from urllib.request import quote
 6 import requests
 7 from bs4 import BeautifulSoup
 8 key_word = quote(input('请输入你喜欢的电影名: '),encoding='gbk')
 9 #key_word = quote('疯狂的外星人',encoding='gbk')
10 res = requests.get('http://s.ygdy8.com/plus/so.php?typeid=1&keyword={}'.format(key_word))
11 res.encoding='gbk'
12 html = res.text
13 soup = BeautifulSoup(html,'html.parser')
14 check_none = soup.find('div',class_='co_content8').find('table')
15 
16 if check_none:
17     item = soup.find('td',width='55%').find('b').find('a')
18     my_url = 'https://www.ygdy8.com'+item['href']
19 
20     res = requests.get(my_url)
21     res.encoding='gbk'
22     html = res.text
23     soup = BeautifulSoup(html,'html.parser')
24     item = soup.find('td',style='WORD-WRAP: break-word').find('a')
25     print(item.text)
26 else:
27     print('没有找到你喜欢的电影')
28 
29 
30 '''
31 执行结果如下:
32 请输入你喜欢的电影名: 0976222
33 没有找到你喜欢的电影
34 
35 请输入你喜欢的电影名: 齐天大圣
36 ftp://ygdy8:ygdy8@yg45.dydytt.net:7387/阳光电影www.ygdy8.com.齐天大圣之大闹龙宫.HD.720p.国语中字.mkv
37 '''
38 '''
39 import requests
40 from bs4 import BeautifulSoup
41 from urllib.request import quote
42 #quote()函数,可以帮我们把内容转为标准的url格式,作为网址的一部分打开
43 
44 movie = input('你想看什么电影呀?')
45 gbkmovie = movie.encode('gbk')
46 #将汉字,用gbk格式编码,赋值给gbkmovie
47 url = 'http://s.ygdy8.com/plus/so.php?typeid=1&keyword='+quote(gbkmovie)
48 #将gbk格式的内容,转为url,然后和前半部分的网址拼接起来。
49 res = requests.get(url)
50 #下载××电影的搜索页面
51 res.encoding ='gbk'
52 #定义res的编码类型为gbk
53 soup_movie = BeautifulSoup(res.text,'html.parser')
54 #解析网页
55 urlpart = soup_movie.find(class_="co_content8").find_all('table')
56 # print(urlpart)
57 
58 if urlpart:
59     urlpart = urlpart[0].find('a')['href']
60     urlmovie = 'https://www.ygdy8.com/' + urlpart
61     res1 = requests.get(urlmovie)
62     res1.encoding = 'gbk'
63     soup_movie1 = BeautifulSoup(res1.text,'html.parser')
64     urldownload = soup_movie1.find('div',id="Zoom").find('span').find('table').find('a')['href']
65     print(urldownload)
66 else:
67     print('没有' + movie)
68     # 有些电影是查询不到没下载链接的,因此加了个判断
69 '''
 1 搜索到电影的html
 2 
 3 <div class="co_content8">
 4 <ul>
 5 <table border="0" width="100%">
 6 <tbody>
 7 <tr height="24">
 8 <td width="6%" align="center">
 9 <img src="/img/file.gif" width="18" height="17">
10 </td>
11 <td width="55%"><b><a href="/html/gndy/jddy/20190206/58170.html">2018年奇幻动作《
12 <font color="red">
13 齐天大圣
14 </font>
15 之大闹龙宫》HD国语中字</a></b>
16 </td>
17 </tr>
18 <tr>
19 <td height="56" colspan="3">   [
20 <font color="red">
21 齐天大圣
22 </font>
23 之大闹龙宫][HD-mkv.720p.国语中字][2018年奇幻动作]
24 ◎译 名 ◎片 名 <font color="red">
25 齐天大圣
26 </font>之大闹龙宫 ◎年
27 代 2019 ◎产 地 中国
28 ◎类 别 动作/奇幻/武侠 ◎语
29 言 普通话 ◎字 幕 中文
30 ◎上映日期
31 2019-02-01(中国)
32 ◎文件格式 x264 + aac
33 ◎视频尺寸 1280 x 720
34 ◎文件<font color="#8F8C89">
35 (2019-02-05)
36 </font>
37 </td>
38 </tr>
39 <tr>
40 <td height="2" colspan="4" background="/img/writerbg.gif">
41 </td>
42 </tr>
43 </tbody>
44 </table>
45 </ul>
46 </div>
47 
48 
49 没有搜索到电影的html
50 
51 <div class="co_content8">
52 <ul>
53 共0页/0条记录
54 </ul>
55 </div>
 
原文地址:https://www.cnblogs.com/www1707/p/10692365.html