import requests
from lxml import etree
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36',
'Host': 'movie.douban.com'
}
url = "https://movie.douban.com/cinema/nowplaying/shanghai/"
response = requests.get(url,headers=headers)
text = response.text
html = etree.HTML(text)
ul = html.xpath("//ul[@class='lists']")[0]
#print(etree.tostring(ul, encoding="utf-8").decode("utf-8"))
lis = ul.xpath("./li")
movies = []
for li in lis:
title = li.xpath("@data-title")[0]
movie = {
"title":title
}
movies.append(movie)
print(movies)
[{'title': '流浪地球'}, {'title': '疯狂的外星人'}, {'title': '飞驰人生'}, {'title': '熊出没·原始时代'}, {'title': '一吻定情'}, {'title': '新喜剧之王'}, {'title': '今夜在浪漫剧场'}, {'title': '小猪佩奇过大年'}, {'title': '廉政风云'}, {'title': '神探蒲松龄'}, {'title': '白蛇:缘起'}, {'title': '密室逃生'}, {'title': '蓝色生死恋'}, {'title': '朝花夕誓'}, {'title': '五十米之恋'}, {'title': '瑞雪兆丰年'}, {'title': '大黄蜂'}, {'title': '钢铁飞龙之奥特曼崛起'}, {'title': '死侍2:我爱我家'}, {'title': '掠食城市'}, {'title': '养家之人'}, {'title': '蜘蛛侠:平行宇宙'}, {'title': '一条狗的回家路'}]