python爬虫第3关项目一键下电影

系统 1508 0

要求:
实现功能:用户输入喜欢的电影名字,程序即可在电影天堂https://www.ygdy8.com爬取电影所对应的下载链接,并将下载链接打印出来。

            
              import requests
from bs4 import BeautifulSoup
from urllib.request import quote
#quote()函数,可以帮我们把内容转为标准的url格式,作为网址的一部分打开
movie=input('你想看什么电影呀?')
gbkmovie=movie.encode('gbk')
URL='http://s.ygdy8.com/plus/so.php?typeid=1&keyword='+quote(gbkmovie)
res=requests.get(URL)
res.encoding='gbk'
html=res.text 
soup=BeautifulSoup(html,'html.parser')
check_none=soup.find('div',class_='co_content8').find('table')

if check_none:
    item=soup.find('td',width="55%").find('b').find('a')['href']
    my_url='https://www.ygdy8.com'+item

    res=requests.get(my_url)
    res.encoding='gbk'
    html=res.text 
    soup=BeautifulSoup(html,'html.parser')
    item=soup.find('td',style="WORD-WRAP: break-word").find('a')['href']#注意WORD-WRAP:后面有一个空格,不然运行不过
    print(item)
else:
    print('没有找到你喜欢的电影')


            
          

运行结果为:

            
              你想看什么电影呀?一条狗的使命
ftp://ygdy8:ygdy8@yg90.dydytt.net:8593/阳光电影www.ygdy8.com.一条狗的使命2.BD.720p.中英双字幕.mkv

            
          

参考答案:

            
              import requests
from bs4 import BeautifulSoup
from urllib.request import quote
#quote()函数,可以帮我们把内容转为标准的url格式,作为网址的一部分打开

movie = input('你想看什么电影呀?')
gbkmovie = movie.encode('gbk')
#将汉字,用gbk格式编码,赋值给gbkmovie
url = 'http://s.ygdy8.com/plus/so.php?typeid=1&keyword='+quote(gbkmovie)
#将gbk格式的内容,转为url,然后和前半部分的网址拼接起来。
res = requests.get(url)
#下载××电影的搜索页面
res.encoding ='gbk'
#定义res的编码类型为gbk
soup_movie = BeautifulSoup(res.text,'html.parser')
#解析网页
urlpart = soup_movie.find(class_="co_content8").find_all('table')
# print(urlpart)

if urlpart:
    urlpart = urlpart[0].find('a')['href']
    urlmovie = 'https://www.ygdy8.com/' + urlpart
    res1 = requests.get(urlmovie)
    res1.encoding = 'gbk'
    soup_movie1 = BeautifulSoup(res1.text,'html.parser')
    urldownload = soup_movie1.find('div',id="Zoom").find('span').find('table').find('a')['href']
    print(urldownload)
else:
    print('没有' + movie)
    # 有些电影是查询不到没下载链接的,因此加了个判断

            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论