在选择标签时,BeautifulSoup无法识别name atribute的值。

问题描述 投票:0回答:1

我试图从一个网站上获取一些下拉列表选项,但我无法让BeautifulSoup在select标签上读取name atribute的值。

我的列表有这样的标签。

第一个列表:

<select id="2" name="ComboEstado" onchange="Executar(this)" style="color:#003366; background:#cccccc; font-size:10px">

第二个列表:

<select id="2" name="ComboMunicipio" onchange="ExecMunicipio()" style="color:#003366; background:#cccccc; font-size:10px">

我的代码试图从第一个列表中读取文本和值。

estados = soup.find(name="ComboEstado", id="2").find_all("option")
for estado in estados:
    print(estado.text, estado['value'])

(这只有在我使用onlye soup.find(id="2")的情况下才有效, 但由于两个列表都有这个属性, 我不认为这是一个好的标识符)

我收到的信息。

属性错误:'NoneType'对象没有属性'find_all'。

也已经尝试使用这个。

soup.find("name"="ComboEstado", id="2")

然后收到了这个:

文件"",第1行 estados = soup.find("name"="ComboEstado", id="2").find_all("option") ^ SyntaxError: 关键字不能是表达式。

也试过了。

soup.find("ComboEstado", id="2")

soup.find("ComboEstado")

两次都出现了同样的第一个错误

任何想法,我在这里错过了什么?先谢谢你了。

python beautifulsoup namespaces find findall
1个回答
0
投票

试着识别这样的列表。

list1=soup.find("select",{"name":"ComboEstado"})
list2=soup.find("select",{"name":"ComboMunicipio"})
© www.soinside.com 2019 - 2024. All rights reserved.