【发布时间】:2020-08-16 00:24:01
【问题描述】:
我正在尝试从网站检索一些下拉列表选项,但我无法让 BeautifulSoup 从选择标签上的名称属性中读取值。
我的列表有这个标签:
第一个列表:
<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"),但由于两个列表都有此属性,我认为这不是一个好的标识符)
我收到的消息:
----------------------------------- ---------------------------- AttributeError Traceback(最近调用 最后)在 ----> 1 estados = soup.find(name="ComboEstado" ,id="2").find_all("option") 2 用于 estados 中的 estado: 3 #nomes e codigos dos estados 4 print(estado.text, estado['value'])
AttributeError: '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