Python ';非类型';对象没有属性';xpath';使用请求时
但我犯了这样一个错误:Python ';非类型';对象没有属性';xpath';使用请求时,python,python-requests,Python,Python Requests,但我犯了这样一个错误: import requests from lxml import etree url = 'https://movie.douban.com/subject/1292052/' data = requests.get(url).text s=etree.HTML(data) film=s.xpath('//*[@id="content"]/h1/span[1]/text()') print('film_title:',film) 回溯(最近一次呼叫最后一次): 文
import requests
from lxml import etree
url = 'https://movie.douban.com/subject/1292052/'
data = requests.get(url).text
s=etree.HTML(data)
film=s.xpath('//*[@id="content"]/h1/span[1]/text()')
print('film_title:',film)
回溯(最近一次呼叫最后一次):
文件“”,第8行,在
film=s.xpath('/*[@id=“content”]/h1/span[1]/text()'))
AttributeError:“非类型”对象没有属性“xpath”
我不知道为什么会发生这种情况以及如何纠正它。您可以添加一个标题来模拟浏览器 headers={'user-agent':'Mozilla/5.0(windowsnt 6.1;WOW64)AppleWebKit/535.1(KHTML,比如Gecko)Chrome/14.0.835.163 Safari/535.1'} url='1〕https://movie.douban.com/subject/1292052/"
data=requests.get(url,headers=headers).text因为您得到的响应是418。请检查此“使用raise_获取响应对象的_状态”,了解您获得的HTTP状态以及非2XX代码的情况下如何处理
Traceback (most recent call last):
File "<ipython-input-5-4e3f3aa89a1c>", line 8, in <module>
film=s.xpath('//*[@id="content"]/h1/span[1]/text()')
AttributeError: 'NoneType' object has no attribute 'xpath'