Python:从 'value' HTML 仅获取 'input'(Mechanize + BeautifulSoup)

标签 python html get beautifulsoup mechanize

我最近正在使用 Python Mechanize 和 BeautifulSoup,并且学习了如何单击并跟踪链接。 我现在想从 HTML 输入中获取一个值,而且我已经非常接近它了,但是我的代码糟糕! 我想要 print 只是

9574363984643591128220162336881714997023153074560071601385105141859609

来自:

<!-- language: lang-js -->
<input size="100" name="query" value="95743639846435911282201623368817149970231530745600716013851051418596093791193" type="text">

我的代码是:

<!-- language: lang-py -->

response3 = br.follow_link(nr=11)           # follow the link
soup = BeautifulSoup(response3, "lxml")     # read the page
for n in soup.findAll('input'):             # find all <input >
    print n['value']                        # print the "value" of all <input > 

我的代码现在正在打印整个页面的全部 <input>!

但我只想打印第一个 <input> 或带有 name="query" 的输入

最佳答案

通过name属性找到它。

soup.find("input", attrs={"name":"query"})["value"]

关于Python:从 'value' HTML 仅获取 'input'(Mechanize + BeautifulSoup),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43470012/

相关文章:

javascript - 用 fetch 事件替换 service worker 中的 Assets

html - 内容 div 未根据需要调整大小

html - 未定义的 Angular 错误 “Cannot read property ' firstName'”

php - 自定义页面 GET 请求上的 Drupal 干净 url

javascript - 获取重复 GET 变量的查询字符串值

python |如何创建复杂的字典

python - HTTPSConnectionPool(主机 ='s3-us-west-1b.amazonaws.com',端口=443): Max retries exceeded with url

javascript - 我如何使这样的代码在添加到列表中的每个元素之间有中断?

python - PyCharm 类型提示返回类型

linux - Python//读取给定输入后的行