我遇到了BeautifulSoup(更具体地说是xml解析器)的问题,其中看起来将“name”作为标记属性正在重载某些底层函数。
给出以下代码:
#!/usr/bin/env python3
from bs4 import BeautifulSoup
siteconfig="""
<?xml version="1.0" encoding="utf-8"?>
<sites version="180201">
<site name="au" location="oceana">
<addresslist="IPv4">
<address>192.168.1.10/32</address>
<address>192.168.2.10/32</address>
</addresslist>
<addresslist="IPv6">
<address>fc00:07bc:5ae6:75d0::26/128</address>
<address>fc00:07bc:5ae6:75d1::26/128</address>
</addresslist>
</site>
<site name="us" location="americas">
<addresslist="IPv4">
<address>192.168.4.13/32</address>
<address>192.168.5.13/32</address>
</addresslist>
<addresslist="IPv6">
<address>fc00:07bc:5ae6:75d0::45/128</address>
<address>fc00:07bc:5ae6:75d1::45/128</address>
</addresslist>
</site>
</sites>
"""
soup = BeautifulSoup(siteconfig,"xml")
print(soup.find("site", name="us"))
我收到以下错误:
Traceback (most recent call last):
File "./siteConfig.py", line 33, in <module>
print(soup.find("site", name="us"))
TypeError: find() got multiple values for argument 'name'
但如果我将最后一行更改为:
print(soup.find("site", location="americas"))
我得到以下输出:
<site location="americas" name="us">
<addresslist>="IPv4">
<address>192.168.4.13/32</address>
<address>192.168.5.13/32</address>
</addresslist>
<addresslist>="IPv6">
<address>fc00:07bc:5ae6:75d0::45/128</address>
<address>fc00:07bc:5ae6:75d1::45/128</address>
</addresslist>
</site>
为什么会这样?
答案 0 :(得分:4)
name
是find
的第一个参数的名称(在您的情况下,&#34;网站&#34;):
找到(name = None,attrs = {},recursive = True,text = None,** kwargs)bs4.BeautifulSoup实例的方法
使用属性字典soup.find("site", {"name":"us"})
来避免碰撞。