如何从html页面获取输入值
like
<input type="hidden" name="captId" value="AqXpRsh3s9QHfxUb6r4b7uOWqMT" ng-model="captId">
我输入了名称 [ name="captId" ] 并需要他的值
import re , urllib , urllib2
a = urllib2.urlopen('http://www.example.com/','').read()
thanx
update 1
我安装了 BeautifulSoup 并使用了它,但出现了一些错误
code
import re , urllib , urllib2
a = urllib2.urlopen('http://www.example.com/','').read()
soup = BeautifulSoup(a)
value = soup.find('input', {'name': 'scnt'}).get('value')
error
“汤 = BeautifulSoup(a)
NameError:名称“BeautifulSoup”未定义”
Using re
解析 xml 或 html 的模块通常被认为是不好的做法。仅在以下情况下使用它you负责您尝试解析的页面。如果不是,要么你的正则表达式非常复杂,要么如果有人替换你的脚本可能会中断<input type="hidden" name=.../>
with <input name="..." type="hidden" .../>
或几乎任何其他东西。
美丽汤是一个 html 解析器:
- 自动修复小错误(未封闭的标签...)
- 构建 DOM 树
- 允许您浏览树、搜索具有特定属性的特定标签
- 可与 Python 2 和 3 一起使用
除非你有充分的理由不这样做,否则你应该使用它而不是re
用于 HTML 解析。
例如假设txt
包含整个页面,查找所有隐藏字段将非常简单:
from bs4 import BeautifulSoup
soup = BeautifulSoup(txt)
hidden_tags = soup.find_all("input", type="hidden")
for tag in hidden_tags:
# tag.name is the name and tag.value the value, simple isn't it ?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)