Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';非类型';对象没有属性';xpath';使用请求时_Python_Python Requests - Fatal编程技术网

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'