【发布时间】:2019-01-29 15:55:51
【问题描述】:
我使用 BeautifulSoup 进行网页抓取已经有一段时间了,这是我第一次遇到这样的问题。我试图在代码中选择数字 101,172,但即使我使用 .find 或 .select,输出始终只是标签,而不是数字。我以前做过类似的数据收集工作,没有遇到任何问题
<div class="legend-block legend-block--pageviews">
<h5>Pageviews</h5><hr>
<div class="legend-block--body">
<div class="linear-legend--counts">
Pageviews:
<span class="pull-right">
101,172
</span>
</div>
<div class="linear-legend--counts">
Daily average:
<span class="pull-right">
4,818
</span>
</div></div></div>
我用过:
res = requests.get(wiki_page, timeout =None)
soup = bs4.BeautifulSoup(res.text, 'html.parser')
ab=soup.select('span[class="pull-right"]')
#print(i)
print(ab)
输出:
[<span class="pull-right">\n<label class="logarithmic-scale">\n<input
class="logarithmic-scale-option" type="checkbox"/>\n Logarithmic scale
</label>\n</span>, <span class="pull-right">\n<label class="begin-at-
zero">\n<input class="begin-at-zero-option" type="checkbox"/>\n Begin at
zero </label>\n</span>, <span class="pull-right">\n<label class="show-
labels">\n<input class="show-labels-option" type="checkbox"/>\n Show
values </label>\n</span>]
另外,我要找的数据号是动态的,所以我不确定Javascript是否会影响BeautifulSoup
【问题讨论】:
-
那么,您是如何尝试获得价值的?我们至少需要一个 sn-p 来检查您的代码中可能出现的问题。
-
奇怪,如果您将 html sn-p 通过 BeautifulSoup 然后为 span 执行 find_all 并在返回时应用 .text 您将获得 101,172..
标签: python html web-scraping beautifulsoup python-beautifultable