workbook = openpyxl.Workbook()
sheet = workbook.active
sheet['A1'] = '序号'
sheet['B1'] = '电影名'
sheet['C1'] = '海报'
sheet['D1'] = '年份'
sheet['E1'] = '地区'
sheet['F1'] = '类型'
sheet['G1'] = '评分'
sheet['H1'] = '评价人数'
sheet['I1'] = '总体评价'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}
index = 1
for start_page in range(0, 250, 25):
response = requests.get(f"https://movie.douban.com/top250?start={start_page}", headers=headers)
html = response.text
soup = BeautifulSoup(html, "html.parser")
items = soup.find_all("div", attrs={"class": "item"})
for item in items:
post = item.find("img").get('src')
name = item.find('span', class_="title").text
infos = item.find('p', class_='').text.split("\n")[2].split("/")
year = infos[0].strip()
location = infos[1].strip()
category = infos[2].strip()
rate = item.find('span', class_='rating_num').text
stars = item.find('div', class_='star')
rate_people = stars.contents[7].text[:-3]
review = ""
if item.find('span', class_='inq') is not None:
review = item.find('span', class_='inq').text
sheet.append([index, name, post, year, location, category, rate, rate_people, review])
index = index + 1
workbook.save('./files/douban_top250.xlsx')