Python爬取豆瓣电影TOP250数据

包含排名,标题,评分,链接
import urllib.request as urlrequest
from bs4 import BeautifulSoup
top250_url = "https://movie.douban.com/top250?start={}&filter="

with open('douban_250.csv','w') as outputfile:
for i in range(10):
start = i*25
url_visit = top250_url.format(start)
crawl_content = urlrequest.urlopen(url_visit).read()
http_content = crawl_content.decode('utf8')
soup = BeautifulSoup(http_content,'html.parser')
all_item_divs = soup.find_all(class_='item')

for each_item_div in all_item_divs:
pic_div = each_item_div.find(class_='pic')
item_href = pic_div.find('a')['href']
item_name = pic_div.find('img')['alt']
info_div = each_item_div.find(class_='bd')
item_average = info_div.find(class_='rating_num').text
item_rank = pic_div.find('em').text
outputfile.write('{},{},{},{}\n'.format(item_rank,item_name,item_average,item_href))