from lxml import *
import requests
url="https://book.douban.com/"
header={"User-Agent": "Mozilla/5.0...."}
一、报错’NoneType’ object has no attribute ‘xpath’,------由于反扒机制,需要加上请求头
html= requests.get(url,headers=header)
#二、 报错can only parse strings,------加上.text
s=etree.HTML(html.text)
三、报错’lxml.etree._Element’ object has no attribute ‘strip’,-------添加/text()就能提取正确内容
titles=s.xpath('//*[@id="content"]/div/div[1]/div[1]/div[2]/div[1]/div/ul[2]/li/div[2]/div[1]/a/text()')
for title in titles:
print(title.strip())
四、最后结果


1665

被折叠的 条评论
为什么被折叠?



