欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

豆瓣Top250

程序员文章站 2022-04-15 13:55:53
1 """ 2 爬取豆瓣电影TOP250 - 完整示例代码 3 """ 4 5 import codecs 6 7 import requests 8 from bs4 import BeautifulSoup 9 10 DOWNLOAD_URL = 'http://movie.douban.com ......
 1 """
 2 爬取豆瓣电影top250 - 完整示例代码
 3 """
 4 
 5 import codecs
 6 
 7 import requests
 8 from bs4 import beautifulsoup
 9 
10 download_url = 'http://movie.douban.com/top250/'
11 
12 
13 def download_page(url):
14     return requests.get(url, headers={
15         'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_11_2) applewebkit/537.36 (khtml, like gecko) chrome/47.0.2526.80 safari/537.36'
16     }).content
17 
18 
19 def parse_html(html):
20     soup = beautifulsoup(html)
21     movie_list_soup = soup.find('ol', attrs={'class': 'grid_view'})
22 
23     movie_name_list = []
24 
25     for movie_li in movie_list_soup.find_all('li'):
26         detail = movie_li.find('div', attrs={'class': 'hd'})
27         movie_name = detail.find('span', attrs={'class': 'title'}).gettext()
28 
29         movie_name_list.append(movie_name)
30 
31     next_page = soup.find('span', attrs={'class': 'next'}).find('a')
32     if next_page:
33         return movie_name_list, download_url + next_page['href']
34     return movie_name_list, none
35 
36 
37 def main():
38     url = download_url
39 
40     with codecs.open('movies', 'wb', encoding='utf-8') as fp:
41         while url:
42             html = download_page(url)
43             movies, url = parse_html(html)
44             fp.write(u'{movies}\n'.format(movies='\n'.join(movies)))
45 
46 
47 if __name__ == '__main__':
48     main()

原文链接:

感谢原po。