我有一个python脚本,该脚本使用urllib2提出请求,然后在var中存储网页的整个源代码:
source = urlopen(request).read().decode()
假设source
变量中有以下HTML输入
<input name="form1" type="hidden" value="value1">
如何获得我的VAR中包含的该输入的值?我可以有一个示例代码吗?
编辑:
所建议的,像这样的美丽套件应该有效吗?
soup = BeautifulSoup(source, 'html.parser')
for value in soup.find(name='value1'):
value = value.get('value')
您需要使用美丽的小组。因此,假设您要提取value
属性的值。这是您要做的:
import BeautifulSoup
import urllib2
request = "http://example.com"
source = urllib2.urlopen(request).read().decode()
# Or you can test with:
# source = "<input name='form1' type='hidden' value='value1'>"
soup = BeautifulSoup(source, "html.parser")
value = soup.find("input", {"name": "form1"}).get("value")