从 python var 中包含的页面源获取输入
Get input from page source contained in python var
我有一个 Python 脚本,它使用 urllib2 发出请求,并使用 var 存储网页的整个源代码:
source = urlopen(request).read().decode()
假设source
变量中有如下html输入
<input name="form1" type="hidden" value="value1">
如何获取我的 var 中包含的输入值?我可以提供一个示例代码吗?
编辑:
按照建议,这样的 BeautifulSoup 代码应该有效吗?
soup = BeautifulSoup(source, 'html.parser')
for value in soup.find(name='value1'):
value = value.get('value')
您需要使用BeautifulSoup。因此,假设您要提取 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")
我有一个 Python 脚本,它使用 urllib2 发出请求,并使用 var 存储网页的整个源代码:
source = urlopen(request).read().decode()
假设source
变量中有如下html输入
<input name="form1" type="hidden" value="value1">
如何获取我的 var 中包含的输入值?我可以提供一个示例代码吗?
编辑:
按照建议,这样的 BeautifulSoup 代码应该有效吗?
soup = BeautifulSoup(source, 'html.parser')
for value in soup.find(name='value1'):
value = value.get('value')
您需要使用BeautifulSoup。因此,假设您要提取 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")