# Author:Nimo_Ding
'''
我们想要实现这样的功能:用户输入喜欢的电影名字,
程序即可在电影天堂https://www.ygdy8.com爬取电影所对应的下载链接,并将下载链接打印出来。
'''
import requests
from bs4 import BeautifulSoup
from urllib.request import quote
# quote()函数,可以帮我们把内容转为标准的url格式,作为网址的一部分打开
# 输入变形金刚的话,就会转成这样:%B1%E4%D0%CE%BD%F0%B8%D5
movie = input("请输入需要查找的电影:")
gbk_movie = movie.encode('gbk')
url = "http://s.ygdy8.com/plus/s0.php?typeid=1&keyword="+quote(gbk_movie)
res = requests.get(url)
res.encoding='gbk'
soup_movie = BeautifulSoup(res.text,'html.parser')
urlpart = soup_movie.find(class_='co_content8').find_all('table')
if urlpart == []:
print("没有找到此影片~")
else:
urlmovie = 'https://www.ygdy8.com/' + urlpart[0].find('a')['href']
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('tbody').find('a')['href']
print('电影:\n{}\n网页链接为:\n{}下载链接为:\n{}'.
format(movie,url,urldownload))