【发布时间】:2018-05-31 04:43:47
【问题描述】:
<a href="/news/2018/05/israeli-army-projectiles-fired-israel-gaza-180529051139606.html">
<h2 class="top-sec-title">
Israel launches counterattacks in Gaza amid soaring tensions
</h2>
</a>
我想使用 h2 的类,即“top-sec-title”,并用 a 的 href 抓取 h2 上的文本。
下面的示例是我在下面处理的 html 有一个 a 标签类,它帮助我在下面的情况下获得了其子元素中的文本,即 h3:
<a class="gs-c-promo-heading gs-o-faux-block-link__overlay-link gel-pica-bold nw-o-link-split__anchor" href="/news/world-us-canada-44294366">
<h3 class="gs-c-promo-heading__title gel-pica-bold nw-o-link-split__text">
Hurricane Maria 'killed 4,600 in Puerto Rico'
</h3>
</a>
下面的代码是我用来从上面的html源中提取数据的。
news = soup.find_all('a', attrs={'class':'gs-c-promo-heading gs-o-faux-block-
link__overlay-link gel-pica-bold nw-o-link-split__anchor'})
for item in news:
print(item.get(href))
print(item.text)
【问题讨论】:
标签: html python-3.x beautifulsoup