此爬虫程序基于requests-BeautifulSoup技术路线,对豆瓣电影Top250的电影进行信息的爬取,整个程序解析点击打开链接,查看此网页源代码,逐一匹配电影名,评价人数,分数,链接,一句话影评并输出。
获取网页Html链接:
def getHTML(url):#获取链接Html
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ''
打印爬取的所有信息:
def print_info(lis):
count = 0
for movie in lis:
count += 1
flist = 'Top{}\n电影名:{}\n评价人数:{}\n分数:{}\n链接:{}\n一句话影评:{}\n'
print(flist.format(count, movie[0], movie[1], movie[2], movie[3], movie[4]))
解析Html页面并保存下来:
parasepage(html, lis):
soup = BeautifulSoup(html, 'html.parser')