Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BeautifulSoup 4解析属性错误_Python_Beautifulsoup - Fatal编程技术网

Python BeautifulSoup 4解析属性错误

Python BeautifulSoup 4解析属性错误,python,beautifulsoup,Python,Beautifulsoup,我试图解析HTML文档,但bs4无法解析特定标记中的属性: 乌尔巴纳固定装置 土粒 当我打印时,错误 AttributeError:“tuple”对象没有属性“items”` 我打印的标记和属性:`select:(u'style',u'class',u'name')` 而不是(例如):'input:{u'type':u'hidden',u'name':u'Immobile\u Note',u'value':u'Ubicazione occuzione',u'id':u'Immobile\u

我试图解析HTML文档,但bs4无法解析特定标记中的属性:


乌尔巴纳固定装置
土粒
当我打印时,错误

AttributeError:“tuple”对象没有属性“items”`
我打印的标记和属性:`select:(u'style',u'class',u'name')`
而不是(例如):'input:{u'type':u'hidden',u'name':u'Immobile\u Note',u'value':u'Ubicazione occuzione',u'id':u'Immobile\u Note'}`

更新: 如果我尝试
soup.find_all(attrs={'id':'somevalue'})
它会失败,因为尝试访问树的所有属性

如果我尝试:

s=BeautifulSoup(“”)
乌尔巴纳固定装置
土粒
""")
解析器将正确检测它:

select:{'id':'TipoImmobileDaNonImportare','style':'width:100%,'class':['inputNormal'],'name':'TipoImmobileDaNonImportare'}
我尝试用lxml解析器和html5lib解析器解析它,但结果是一样的

谢谢你的回复

编辑: 多亏了Amanda,但我的代码中出现了一个错误,我尝试在
tag.attrs
中存储一个touple对象,因为此代码正在从bs3移植到bs4!
谢谢。

我不完全确定您在这里尝试使用Beautiful Soup访问什么,但是如果您想获得select或options的属性,可以执行以下操作:

html = """<select class="inputNormal" id="TipoImmobileDaNonImportare" name="TipoImmobileDaNonImportare" style="width:100%">
        <option value=""></option>
        <option value="unità immobiliare urbana">unità immobiliare urbana</option>
        <option value="particella terreni">particella terreni</option></select>"""

soup = BeautifulSoup(html)
或使用以下选项显示所有选项的属性:

for option in soup.find_all('option'):
    print option.attrs
或者,如果要查找可用项目的名称,请使用:

for option in soup.find_all('option'):
    print option.text
或者,如果需要选项值而不是显示的文本,请使用:

for option in soup.find_all('option'):
    print option['value']

如果这没有帮助,也许您可以给出一个您期望的输出示例

我不完全确定您在这里尝试使用Beautiful Soup访问什么,但是如果您想获得选择或选项的属性,您可以执行以下操作:

html = """<select class="inputNormal" id="TipoImmobileDaNonImportare" name="TipoImmobileDaNonImportare" style="width:100%">
        <option value=""></option>
        <option value="unità immobiliare urbana">unità immobiliare urbana</option>
        <option value="particella terreni">particella terreni</option></select>"""

soup = BeautifulSoup(html)
或使用以下选项显示所有选项的属性:

for option in soup.find_all('option'):
    print option.attrs
或者,如果要查找可用项目的名称,请使用:

for option in soup.find_all('option'):
    print option.text
或者,如果需要选项值而不是显示的文本,请使用:

for option in soup.find_all('option'):
    print option['value']

如果这没有帮助,也许您可以给出一个您期望的输出示例

在这种情况下,您试图解析或访问什么?在这种情况下,您试图解析或访问什么?