2018.12.23更新:假如你使用的是anaconda安装的lxml库,可能需要更改 以下代码 ,直接是python3.7+pycharm的话我这边我的代码是没有问题能运行的。
from lxml import html
#导入包不在是import etree
...
...
htmlelement = html.etree.HTML(text)
#这边变量的名字得改一下,HTML解析的方法也得改
#不保证有用,因为博主没试过。也是看文档才发现这个改了
import requests #需要安装request库和lxml库
from lxml import etree
headers = {
'User_Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK\
it/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
'Referer':'https://www.baidu.com/s?ie=UTF-8&wd=douban'
}
url = "https://movie.douban.com/cinema/nowplaying/nanchang/"
#修改豆瓣网的正在上映url,最后的地区拼音不一样
res = requests.get(url)
text = res.text
html = etree.HTML(text)
ul = html.xpath("//ul[@class='lists']")[0]
lis = ul.xpath("./li")
for li in lis:
title = li.xpath("@data-title")[0]
score = li.xpath("@data-score")[0]
if score == "0":
score = "暂无评分"
print("电影名:%s"%title)
print("电影评分:%s"%score)
print("")
改进版:
import requests
from lxml import etree
headers = {
'User_Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK\
it/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
'Referer':'https://www.baidu.com/s?ie=UTF-8&wd=douban'
}
urlori = "https://movie.douban.com/cinema/nowplaying/"
location = input("输入你所在市名拼音")
url = urlori+location
print(url)
res = requests.get(url)
text = res.text
html = etree.HTML(text)
ul = html.xpath("//ul[@class='lists']")[0]
lis = ul.xpath("./li")
for li in lis:
title = li.xpath("@data-title")[0]
score = li.xpath("@data-score")[0]
if score == "0":
score = "暂无评分"
print("电影名:%s"%title)
print("电影评分:%s"%score)
print("")