Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.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提取HTML属性值列表_Python_Html_List_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 使用beautifulsoup提取HTML属性值列表

Python 使用beautifulsoup提取HTML属性值列表,python,html,list,python-3.x,beautifulsoup,Python,Html,List,Python 3.x,Beautifulsoup,给定一个包含元素列表的页面,例如元素,所有元素都有一个公共类,例如类别复选框,如何使用beautifulsoup将所有这些元素的值提取到列表中?例如: <input type="checkbox" class="category-checkbox" value="apples" /> <input type="checkbox" class="category-checkbox" value="pears" /> <input type="checkbox" cla

给定一个包含元素列表的页面,例如
元素,所有元素都有一个公共类,例如
类别复选框
,如何使用beautifulsoup将所有这些元素的值提取到列表中?例如:

<input type="checkbox" class="category-checkbox" value="apples" />
<input type="checkbox" class="category-checkbox" value="pears" />
<input type="checkbox" class="category-checkbox" value="oranges" />

我最终将元素映射到一个新列表:

# assume the html variable is a beautifulsoup object
input_values = [
    element.get('value') for element in
    html.findAll('input', {"class": "category-checkbox"})
]
# category_input_values is now [ 'apples', 'pears', 'oranges' ]

你在发布自己的问题2秒钟后回答了吗?@RafaelCardoso yep:)你知道“回答自己的问题,问答式”按钮,你可以点击它来提问吗?我就是这么做的,以防对别人有帮助。我在研究如何做的时候,没有找到我想要的确切答案。另外,如果有人能想出更好的方法,我很乐意看到。