我只是想知道此错误是由于python中的HTML刮板的响应。它说elementtree尚未初始化,并且缺少根。
如果有人知道这是什么,并且可以告诉我这意味着什么,那将是一个很好的解决方案。谢谢。
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