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:)你知道“回答自己的问题,问答式”按钮,你可以点击它来提问吗?我就是这么做的,以防对别人有帮助。我在研究如何做的时候,没有找到我想要的确切答案。另外,如果有人能想出更好的方法,我很乐意看到。