獲取文本我有一些當前的Python代碼應該從網站的某個部分使用HTML標記所在位置的xpath獲取HTML代碼。嘗試從網站的某個部分使用lxml.html
def wordorigins(word):
pageopen = lxml.html.fromstring("http://www.merriam-webster.com/dictionary/" + str(word))
pbody = pageopen.xpath("/html/body/div[1]/div/div[4]/div/div[1]/main/article/div[5]/div[3]/div[1]/div/p[1]")
etybody = lxml.html.fromstring(pbody)
etytxt = etybody.xpath('text()')
etytxt = etytxt.replace("<em>", "")
etytxt = etytxt.replace("</em>", "")
return etytxt
此代碼返回該錯誤有關期待一個字符串或緩衝區:
Traceback (most recent call last):
File "mott.py", line 47, in <module>
print wordorigins(x)
File "mott.py", line 30, in wordorigins
etybody = lxml.html.fromstring(pbody)
File "/usr/lib/python2.7/site-packages/lxml/html/__init__.py", line 866, in fromstring
is_full_html = _looks_like_full_html_unicode(html)
TypeError: expected string or buffer
的思考?