学习python之余,想列一个观影清单,每次上豆瓣搜太麻烦了,就爬取一个清单自己使用吧。
直接上代码:
coding:“utf-8”
import requests
import re
import csv
f=open(‘douban.csv’,mode=‘w’,newline=’’)
csvwirter=csv.writer(f)
for num in range(0,250,25):
url=f"https://movie.douban.com/top250?start={num}"
header = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36’
}
resp=requests.get(url,headers=header)
text=resp.text
rel = re.compile(r’
- .?(?P.?).?
(?P.?) .?’
r’(?P.?).?(?P.?)人评价’,re.S)
result=rel.finditer(text)
for i in result:
info=i.groupdict()
info[‘year’]=info[‘year’].strip()
csvwirter.writerow(info.values())
resp.close()
f.close()
print(‘over’)