Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
为什么我的XPATH代码可以在站点上工作,而不能在python编译器中工作?_Python_Xpath - Fatal编程技术网

为什么我的XPATH代码可以在站点上工作,而不能在python编译器中工作?

为什么我的XPATH代码可以在站点上工作,而不能在python编译器中工作?,python,xpath,Python,Xpath,我只是想知道在python中我的HTML刮板的响应中出现了什么错误。它表示elementtree未初始化,并且缺少根。 如果有人知道这是什么,并能告诉我这意味着什么,这将是一个伟大的解决方案。多谢各位 这是我的密码 以下是答案 回溯(最近一次呼叫最后一次): 文件“word.py”,第62行,在 wordtodefine=tree.xpath('/*[@id=“js spellPrompt”]/span').text lxml.etree.\u ElementTree.xpath中的文件“sr

我只是想知道在python中我的HTML刮板的响应中出现了什么错误。它表示elementtree未初始化,并且缺少根。
如果有人知道这是什么,并能告诉我这意味着什么,这将是一个伟大的解决方案。多谢各位

这是我的密码 以下是答案

回溯(最近一次呼叫最后一次):
文件“word.py”,第62行,在
wordtodefine=tree.xpath('/*[@id=“js spellPrompt”]/span').text
lxml.etree.\u ElementTree.xpath中的文件“src\lxml\etree.pyx”,第2292行
文件“src\lxml\etree.pyx”,第1870行,位于lxml.etree.\u ElementTree.\u assertHasRoot
AssertionError:ElementTree未初始化,缺少根

可能是下载问题(缺少标题或用户代理)。如果您尝试使用lxml import etree中的
在本地解析文件,会发生什么;tree=etree.parse(“yourdata.xml”)
?您进行过任何调试吗?例如,
tree
的内容是您期望的吗?tree的内容是我期望的。我将检查下载问题,看看是否可以解析来自我自己计算机的数据。谢谢
import lxml.html
import requests
url =  "https://quizlet.com/495157354/spell"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
response = requests.get(url, headers=headers) #stream=True)
print(response)
response.raw.decode_content = True
tree = lxml.html.parse(response.raw)
wordtodefine = tree.xpath('//*[@id="js-spellPrompt"]/span').text
print(tree.xpath)
print(wordtodefine)
input("Enter")
<Response [200]>
Traceback (most recent call last):
  File "word.py", line 62, in <module>
    wordtodefine = tree.xpath('//*[@id="js-spellPrompt"]/span').text
  File "src\lxml\etree.pyx", line 2292, in lxml.etree._ElementTree.xpath
  File "src\lxml\etree.pyx", line 1870, in lxml.etree._ElementTree._assertHasRoot
AssertionError: ElementTree not initialized, missing root