python 爬虫豆瓣电影(TEXT文章)

import requests
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
    'Referer': 'https://movie.douban.com/cinema/nowplaying/beijing/'
}

url = 'https://movie.douban.com/cinema/nowplaying/beijing/'
response = requests.get(url, headers=headers)
text = response.text

html = etree.HTML(text)


# nodename    选取此节点的所有子节点。
# /           从根节点选取。
# //          从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
# .           选取当前节点。
# ..          选取当前节点的父节点。
# @           选取属性。
# 获取正在上映的电影
ul = html.xpath("//ul[@class='lists']")[0]     # //title[@lang]	选取所有拥有名为 lang 的属性的 title 元素。
lis = ul.xpath("./li")
movies = []

# 获取电影标示
for li in lis:
    title = li.xpath("@data-title")[0]
    score = li.xpath("@data-score")[0]
    duration = li.xpath("@data-duration")[0]
    region = li.xpath("@data-region")[0]
    director = li.xpath("@data-director")[0]
    actors = li.xpath("@data-actors")[0]

    # dianying haibao photo
    thumbnail = li.xpath(".//img/@src")[0]

    movie = {
        "title" : title,
        'score' : score,
        'duration' :duration,
        "region":region,
        'director':director,
        'actors':actors
    }
    movies.append(movie)
    print(movie)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路奇怪

有钱出钱,没钱多出编程主意啊

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值