import re
import requests
import csv
for page in range(0,226,25):
url = "https://movie.douban.com/top250?start=%s&filter="%page
print(url)
header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}
resp = requests.get(url,headers=header)
page_content = resp.text
obj = re.compile('<div class="item">.*?<span class="title">(?P<name>.*?)</span>.*?<p class="">.*?<br>(?P<year>.*?) .*?<div class="star">.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?<span property="v:best" content="10.0"></span>.*?<span>(?P<number>.*?)</span>',re.S)
result = obj.finditer(page_content)
f = open("data.csv",mode="a+",encoding='utf-8')
csvwrite = csv.writer(f)
for i in result:
dic = i.groupdict()
dic['year'] = dic['year'].strip()
csvwrite.writerow(dic.values())
f.close()
resp.close()
犯过的错误:
1.循环要对齐,格式要对齐 2.mode =‘a’ 如果是w就会出现只有一页数据的现象 r 只读模式 w 写模式 a 追加写模式
|