问题遇到的现象和发生背景
python爬虫xpath
问题相关代码,请勿粘贴截图
import requests
from lxml import etree
url = "https://movie.douban.com/chart"
head = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"}
response = requests.get(url,headers=head).text
tree = etree.HtML(response)
li_list = tree.xpath('//div[@class="pl2"]/a')
fp=open('a.txt','w',encoding='utf-8')
#li遍历li_list
for li in li_list:
print(li+'\n')
#把文件写入文件
fp.write(li+'\n')
fp.close()
运行结果及报错内容
Traceback (most recent call last):
File "C:/Users/HY/PycharmProjects/pachong/main.py", line 8, in <module>
tree = etree.HtML(response)
AttributeError: module 'lxml.etree' has no attribute 'HtML'
我的解答思路和尝试过的方法
from lxml import html
selector = html.etree.HTML(text)
我想要达到的结果
etree没有html部分,有点懵逼