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)


 

 

你可能感兴趣的:(python,爬虫,python)