如何修复bs4选择错误:“TypeError:\uyu init_u9()关键字必须是字符串”

2024-04-28 02:10:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本,它使用post请求并获得一个XML作为返回。我需要解析这个XML来知道post请求是否被接受。 我使用bs4来解析它,直到大约一周前,我开始发现一个以前没有遇到的错误:

TypeError: __init__() keywords must be strings

我在同一个文件的其他部分使用了bs4的select函数,但没有得到这个错误,而且我在网上找不到任何关于它的信息。 一开始我以为是版本问题,但我尝试了python3.7和3.6,得到了相同的错误。在

这是用于生成错误的代码:

^{pr2}$

完整错误消息:

Traceback (most recent call last):
  File "EbarInt.py", line 292, in <module>
    resCode = resSoup.select_one('ResultCode').text
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\bs4\element.py", line 1345, in select_one
    value = self.select(selector, namespaces, 1, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\bs4\element.py", line 1377, in select
    return soupsieve.select(selector, self, namespaces, limit, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\soupsieve\__init__.py", line 108, in select
    return compile(select, namespaces, flags).select(tag, limit)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\soupsieve\__init__.py", line 50, in compile
    namespaces = ct.Namespaces(**(namespaces))
TypeError: __init__() keywords must be strings

当我检查的时候res.文本类型I按预期获取类“str”。在

当我记录res.text时,我得到:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><soap:Header><wsa:Action>Trackem.Web.Services/CreateOrUpdateTaskResponse</wsa:Action><wsa:MessageID>urn:uuid:3ecae312-d416-40a5-a6a3-9607ebf28d7a</wsa:MessageID><wsa:RelatesTo>urn:uuid:6ab7e354-6499-4e37-9d6e-61219bac11f6</wsa:RelatesTo><wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To><wsse:Security><wsu:Timestamp wsu:Id="Timestamp-6b84a16f-327b-42db-987f-7f1ea52ef802"><wsu:Created>2019-01-06T10:33:08Z</wsu:Created><wsu:Expires>2019-01-06T10:38:08Z</wsu:Expires></wsu:Timestamp></wsse:Security></soap:Header><soap:Body><CreateOrUpdateTaskResponse xmlns="Trackem.Web.Services"><CreateOrUpdateTaskResult><ResultCode>OK</ResultCode><ResultCodeAsInt>0</ResultCodeAsInt><TaskNumber>18000146</TaskNumber></CreateOrUpdateTaskResult></CreateOrUpdateTaskResponse></soap:Body></soap:Envelope>

Tags: inpyorghttpinit错误lineselect
1条回答
网友
1楼 · 发布于 2024-04-28 02:10:24

更新:beauthoulsoup4.7.1已经发布,修复了默认命名空间问题。参见release notes。您可能只想升级性能修复程序。在

原始答案:


您必须升级到beautifulsoup4.7,它用^{} project替换了简单而有限的内部CSS解析器,这是一个更完整的CSS实现。在

正是该项目存在附加到响应中的某个元素的默认命名空间的问题:

<CreateOrUpdateTaskResponse xmlns="Trackem.Web.Services">

用于构建BeautifulGroup对象树的XML解析器正确地将其作为命名空间字典中的None->;'Trackem.Web.Services'映射进行通信,但是soupsieve代码要求所有命名空间都有一个前缀名(xmlns:prefix),默认命名空间用空字符串标记,而不是None,从而导致此错误。我以issue #68 to the ^{} project的形式报告了这一点。在

这里根本不需要使用select_one,除了元素名之外,您没有使用任何CSS语法。使用soup.find()代替:

^{pr2}$

相关问题 更多 >