python BeautifulSoup获取特定元素 [英] python BeautifulSoup get specific element
本文介绍了python BeautifulSoup获取特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的html代码
if i have an html code like this
<div class="new_info_next">
<input type="hidden" value="133" id="new_id" class="new_id">
<input type="hidden" value="0" id="default_pe" class="default_pe">
</div>
并且我只想在input
的第一行中得到133
我使用BeautifulSoup4尝试此代码
and i want to get only 133
in input
the first line i try this code using BeautifulSoup4
info = soup.find_all("div", {"class": "new_info_next"})
for inpu in info:
for inpu1 in inpu.select('input'):
print inpu1 .get('value')
但输出为
133
0
如何仅获取133
推荐答案
由于您只希望迭代器中的第一个元素,因此直接寻址应该有效:
Since you only want the first element in the iterator, addressing it directly should work:
first = inpu.select('input')[0].get('value')
print(first)
这篇关于python BeautifulSoup获取特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文