2017-04-18 121 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"]