[Python] 纯文本查看 复制代码import requests
from bs4 import BeautifulSoup
import csv
movies_dict ={}
header = ['序号','电影名','评分','推荐语','URL']
for i in range(10):
res = requests.get('https://movie.douban.com/top250?start='+str(i*25)+'&filter=')
bs_res = BeautifulSoup(res.text,'html.parser')
num_all = bs_res.find_all('em',class_='')
title_all = bs_res.find_all('div',class_='hd')
star_all = bs_res.find_all('span',class_='rating_num')
tui_all = bs_res.find_all('span',class_='inq')
url_all = bs_res.find_all('div',class_='pic')
n =len(title_all)
a=0
for b in range(len(title_all)):
movies_list =[]
title = title_all[b].find(class_='title')
#print(num_all[a].text,title.text,star_all[a].text.replace('\n',''),tui_all[a].text,url_all[a].find('a')['href'])
movies_list = [num_all[a].text,title.text,star_all[a].text,tui_all[a].text,url_all[a].find('a')['href']]
a += 1
for e in range(5):
movies_dict[header[e]] = movies_list[e]
with open('movies1.csv','a+',newline='',encoding='utf-8') as movies_file:
movies_csv = csv.DictWriter(movies_file,fieldnames=header)
movies_file.writerow(movies_dict)