gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:51 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com