2017-04-18 142 views
1

我最近使用Python机械化& BeautifulSoup和我学会了如何点击并关注链接。 我现在想从HTML输入中获取一个值,而且我非常接近它,但是我的代码是太可怕了! 我想print只是Python:从'输入'HTML(Mechanize + BeautifulSoup)获取'value'

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"

回答

1

通过name属性搜寻。

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