40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
import requests
|
|
import re
|
|
import csv
|
|
|
|
url = "https://movie.douban.com/top250"
|
|
r = None
|
|
headers = {
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26'}
|
|
try:
|
|
r = requests.get(url, headers=headers)
|
|
r.raise_for_status()
|
|
r.encoding = r.apparent_encoding
|
|
except:
|
|
print("异常")
|
|
|
|
# print(r.text)
|
|
page_context = r.text
|
|
|
|
obj = re.compile(r'<li>.*?<span class="title">(?P<name>.*?)</span>.*?'
|
|
r'<p class="">.*?<br>(?P<year>.*?) .*?'
|
|
r'<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?'
|
|
r'<span>(?P<review>.*?)人评价</span>.*?</li>'
|
|
, re.S) # (?P<name>.*?)分组组名
|
|
result = obj.finditer(page_context)
|
|
f = open('data.csv', mode='w')
|
|
csvwriter = csv.writer(f)
|
|
for i in result:
|
|
dic = i.groupdict()
|
|
dic['year'] = dic['year'].strip()
|
|
csvwriter.writerow(dic.values()) # 写入一行
|
|
f.close()
|
|
print("over!")
|
|
|
|
# print(i.group("name"))
|
|
# print(i.group('year').strip())#去空格
|
|
# print(i.group("score"))
|
|
# print(i.group("review") + '人评论')
|
|
|
|
r.close()
|