python - 在python中从html获取2个值的最佳方法
问题描述
我正在使用 beautifulsoup,我想在 python 中抓取 2 个属性值。
<input type="hidden" name="test" value="123456789">
<input type="hidden" name="test" value="987654321">
我想同时获得这两个值。这是我现在正在使用的代码
number = BeautifulSoup(html, 'html.parser')
final = number.find('input')['name']
soup.find('input')['value']
print(final [2])
解决方案
你正在使用.find()
它只给你第一个标签。您应该使用.find_all()
来获取所有input
标签,然后从中获取值。
from bs4 import BeautifulSoup
text = '''
<input type="hidden" name="test" value="123456789">
<input type="hidden" name="test" value="987654321">
'''
soup = BeautifulSoup(text, 'lxml')
values = [each.attrs['value'] for each in soup.find_all('input')]
# -> ['123456789', '987654321']
推荐阅读
- c++ - 超出三次函数根二等分搜索时间限制
- r - 使用 `+` 运算符将徽标添加到 ggplot 页脚
- php - 在 Laravel 中以 PDF 文件显示二维码
- java - 如何使用布尔方法从购物车中删除对象?
- json - MongoDB,在 $convert(aggregation) 中将带逗号的字符串转换为双精度。无法解析数字“1,533.07”
- android - 如何在android中使用recyclerview显示来自Web服务器的JSON格式数据
- android - Cordova android fcm插件jquery php点击背景通知
- r - 在堆叠的ggplot geom_bar中添加不同颜色的数据标签
- python - 美丽汤。一个链接的搜索优化
- javascript - Javascript文件未加载