【问题标题】:Python: GET only 'value' from 'input' HTML (Mechanize + BeautifulSoup)Python:仅从“输入”HTML 获取“值”(Mechanize + BeautifulSoup)
【发布时间】:2017-09-14 03:57:49
【问题描述】:

我最近在使用 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 > 

我的代码现在正在打印all整个页面的&lt;input&gt;

但我只想打印 first &lt;input&gt; 或带有 name="query" 的输入

【问题讨论】:

    标签: python html get beautifulsoup mechanize


    【解决方案1】:

    通过name 属性找到它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 2016-03-21
      相关资源
      最近更新 更多