爬取豆瓣电影各个类型的电影名称

import requests,re,json
from lxml import etree
from openpyxl import  Workbook
wb = Workbook()
ws = wb.active
ws.append(['电影名称'])
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'
    
}
base_url = 'https://movie.douban.com/chart'
movies_count_url= 'https://movie.douban.com/j/chart/top_list_count?type={}&interval_id=100%3A90'
movies_url = 'https://movie.douban.com/j/chart/top_list?type={}&interval_id=100%3A90&action=&start=0&limit={}'
response = requests.get(url=base_url,headers=headers)
html = etree.HTML(response.text)
span_list = html.xpath('//div[@class="types"]/span')
for span in span_list:
    big_title = span.xpath('./a/text()')[0]
    types_id = span.xpath('./a/@href')[0]
    type_id = re.findall('.*?type=(\d+).*?',types_id)[0]
    # print(big_title,type_id)
    resp = requests.get(url=movies_count_url.format(type_id),headers=headers)
    dict_data = json.loads(resp.text)
    movies_count = dict_data['total']
    # print(movies_count)
    res = requests.get(url=movies_url.format(type_id,movies_count),headers=headers)
    movies_list = json.loads(res.text)
    print(big_title)
    for movies in movies_list:
        lis = []
        movies_name = movies['title']
        lis.append(movies_name)
        ws.append(lis)
wb.save('./data/豆瓣电影大全.xlsx')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值