python 爬取豆瓣网评分前250部电影信息

from bs4 import BeautifulSoup
import requests
import pandas as pd

headers = {
    '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'
}

res_data = []
for i in range(1, int(250 / 25) + 1):
    page = i * 25 - 25
    url = 'https://movie.douban.com/top250?start=' + str(page)
    movie = BeautifulSoup(requests.get(url, headers=headers).text, 'html.parser')
    info_list = movie.findAll('div', {'class': 'info'})
    for div in info_list:
        title = div.find('div', {'class': 'hd'})
        title_text = ''.join(title.get_text().strip().split('\n')[:-1])

        info = div.find('div', {'class': 'bd'})
        info1 = [i.strip() for i in info.find('p').get_text().strip().split('\n')]
        personnel = info1[0]
        type_ = info1[1]
        try:  # 有些电影没有简介
            quote = div.find('p', {'class': 'quote'}).get_text().strip()
        except:
            quote = None

        res_data.append([title_text, yanyuan, leixing, quote])

pd.DataFrame(res_data, columns=['电影', '导演&演员', '时间&分类', '简介']).to_excel('./movieTop250.xlsx', index=False)

上一篇:软件测试经典逻辑题


下一篇:使用CMD临时发送一封邮件