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

2021/7/9 14:35:41

本文主要是介绍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)



这篇关于python 爬取豆瓣网评分前250部电影信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程