如何使用python获取隐藏输入的值?

IBR*_*BRA 6 python urllib2 findall python-2.7

如何从html页面获取输入值

喜欢

<input type="hidden" name="captId" value="AqXpRsh3s9QHfxUb6r4b7uOWqMT" ng-model="captId">
Run Code Online (Sandbox Code Playgroud)

我有输入名称[name ="captId"]并需要他的值

import re , urllib ,  urllib2
a = urllib2.urlopen('http://www.example.com/','').read()
Run Code Online (Sandbox Code Playgroud)

感谢名单


更新1

我安装了BeautifulSoup并使用它但有一些错误

 import re , urllib ,  urllib2
 a = urllib2.urlopen('http://www.example.com/','').read()
 soup = BeautifulSoup(a)
 value = soup.find('input', {'name': 'scnt'}).get('value')
Run Code Online (Sandbox Code Playgroud)

错误

"soup = BeautifulSoup(a)NameError:名称'BeautifulSoup'未定义"

Ser*_*sta 5

使用re模块解析xml或html通常被认为是不好的做法.仅在对要尝试解析的页面负责时才使用它.如果没有,或者您的正则表达式是非常复杂的,或者如果有人取代你的脚本可能会破坏<input type="hidden" name=.../><input name="..." type="hidden" .../>或几乎任何东西.

BeautifulSoup是一个html解析器:

  • 自动修复小错误(未关闭的标签...)
  • 构建一个DOM树
  • 允许您浏览树,搜索具有特定属性的特定标记
  • 可用于Python 2和3

除非你有充分的理由不这样做,否则你应该使用它而不是reHTML解析.

例如,假设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 ?
Run Code Online (Sandbox Code Playgroud)