【问题标题】:Using lxml to process html from requests. TypeError: can't pickle _ElementUnicodeResult objects使用 lxml 处理来自请求的 html。 TypeError:无法腌制 _ElementUnicodeResult 对象
【发布时间】:2014-12-10 17:53:38
【问题描述】:

我正在尝试在页面上的特定 xpath 中找到数据。我可以通过请求访问该页面。我已经通过使用 r.text 将源代码打印到我的屏幕并将显示的文本与我正在寻找的文本进行比较来验证我在正确的页面上。

r.text 返回一个字符串,很难从中提取我想要的信息。我被告知 lxml 是通过 xpath 搜索信息的方式。不幸的是,我遇到了类型错误。

from lxml import html
import requests

payload = {'login_pass': 'password', 'login_user': 'username','submit':'go'}
r = requests.get("website", params=payload)

print r.encoding
tree = html.fromstring(r.text)
print tree
print tree.text_content()

返回

UTF-8
<Element html at 0x10dab8d08>

Traceback (most recent call last):
  File "/Users/Me/Documents/PYTHON/GetImageAsPdf/ImageToPDF_requests_beta.py", line 11, in <module>
    print tree.text_content()
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/PyShell.py", line 1343, in write
    return self.shell.write(s, self.tags)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/rpc.py", line 595, in __call__
    value = self.sockio.remotecall(self.oid, self.name, args, kwargs)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/rpc.py", line 210, in remotecall
    seq = self.asynccall(oid, methodname, args, kwargs)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/rpc.py", line 225, in asynccall
    self.putmessage((seq, request))
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/rpc.py", line 324, in putmessage
    s = pickle.dumps(message)
  File "/usr/local/Cellar/python/2.7.6/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle _ElementUnicodeResult objects

我尝试检查标题

r.headers

返回

{'charset': 'utf-8',
 'x-powered-by': 'PHP/5.3.3',
 'transfer-encoding': 'chunked',
 'set-cookie': 'PHPSESSID=c6i7kph59nl9ocdlkckmjavas1; path=/, LOGIN_USER=deleted; expires=Tue, 15-Oct-2013 15:12:08 GMT; path=/',
 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT',
 'server': 'Apache/2.2.15 (CentOS)',
 'connection': 'close',
 'pragma': 'no-cache',
 'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
 'date': 'Wed, 15 Oct 2014 15:12:09 GMT',
 'content-type': 'text/html; charset=UTF-8'}

我的目标是能够像这样通过 xpath 搜索树:

quantity = tree.xpath('/html/body/form[1]/table[3]/tbody[1]/tr/td[2]/table/tbody/tr/td[1]/table/tbody/tr/td/table[1]/tbody/tr[1]/td[2]/strong')

你能帮我确定我哪里出错了吗?

【问题讨论】:

  • 您从哪里运行此代码?空闲?
  • 是的,我正在使用 IDLE。蟒蛇 2.7.6。 Mac OSx 10.8.5。我使用的是与自制软件一起安装时使用 python 内置的 IDLE。
  • 看起来该错误正在发生,因为 IDLE 需要腌制您尝试运行的命令的内容并将其发送到另一个进程。如果您直接从 CLI 运行此脚本,是否可以正常工作?
  • 是的。这似乎成功了。我使用该数量线的目标不是通过该 xpath 获取任何数据,但我认为这是另一个问题。树不再返回错误,所以我离成功更近了一步。谢谢。

标签: python xpath lxml python-requests


【解决方案1】:

您应该能够将 _ElementUnicodeResult 对象转换为常规的、可提取的 unicode 字符串。

使用 Python 2,只需将其包装为 unicode(),例如print unicode(tree.text_content())

使用 Python 3,只需将其包装在 str() 中,例如str(tree.text_content())

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 2019-03-03
    • 2017-10-23
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多