python - 如何在段落文本中使用其他一些标签刮取段落标记中的文本?
问题描述
我想在段落标签中抓取信息。这
tag 里面还有一些其他的标签。我将在下面的代码中向您展示。
这里是
这是要抓取的html页面:
<div class="thecontent">
<p>Here’s the schedule of matches for the weekend.</p>
<p> </p>
<p><strong>Saturday, August 17</strong></p>
<p>Achara vs. Buad, <a href="@">ftv</a>, <a href="https://someothertv">HTlive</a>, <a href="http://www.anothertv target="_blank">Se</a> — Have enjoy it and celebrate it</p>
<p>pritos vs. baola, <a href="@">ftv</a>, <a href="https://someothertv">HTlive</a>, <a href="http://www.anothertv target="_blank">Se</a> — Have enjoy it and celebrate it</p>
<p>timpao vs. quadrsa, <a href="@">ftv</a>, <a href="https://someothertv">HTlive</a>, <a href="http://www.anothertv target="_blank">Se</a> — Have enjoy it and celebrate it</p>
<p><strong>Sunday, August 18</strong></p>
<p>Achara vs. timpao, <a href="@">ftv</a>, <a href="https://someothertv">HTlive</a>, <a href="http://www.anothertv target="_blank">Se</a> — Have enjoy it and celebrate it</p>
<p>pritos vs. qaudra, <a href="@">ftv</a>, <a href="https://someothertv">HTlive</a>, <a href="http://www.anothertv target="_blank">Se</a> — Have enjoy it and celebrate it</p>
<p>timpao vs. Buad, <a href="@">ftv</a>, <a href="https://someothertv">HTlive</a>, <a href="http://www.anothertv target="_blank">Se</a> — Have enjoy it and celebrate it</p>
<p> </p>
<p><strong>Monday, August 19</strong></p>
<p>Achara vs. Buad, <a href="@">ftv</a>, <a href="https://someothertv">HTlive</a>, <a href="http://www.anothertv target="_blank">Se</a> — Have enjoy it and celebrate it</p>
</p>
<p> </p></div></body></html>
我使用了以下python代码:
import bs4,requests
getnwp = requests.get('https://url')
nwpcontent = getnwp.content
sp2 = bs4.BeautifulSoup(nwpcontent, 'html5lib')
pta = sp2.find('div', class_ = 'thecontent').find_all('p')
for i in range(len(pta)):
if pta[i].get_text().find("vs") != -1:
print (pta[i].get_text())
有了上面的信息,我想只提取团队之间的匹配以及它发生的日期。和下面的小消息:
8月17日星期六
Achara vs. timpao,——享受它并庆祝它
pritos vs. baola,——享受它并庆祝它
timpao vs. quadrsa——享受它并庆祝它
8月18日星期日
Achara vs. timpao,——享受它并庆祝它
pritos vs. qaudra,——享受它并庆祝它
timpao vs. Buad——享受它并庆祝它
8月19日星期一
Achara vs. Buad,-享受它并庆祝它
我的意思是我不想要有关电视广播的信息(或锚标签中的信息)。
解决方案
看起来有内容的段落还包括提示“, - 享受它并庆祝它”,所以当你检索它的内容时,它总是被添加。您可以做的是通过执行类似的操作来消除字符串的拖尾
if len(pta[i] > 33):
pta[i].get_text()[:-33]
这样,您将删除结果字符串的最后 33 个字符。
推荐阅读
- mysql - 连接 Spotfire Azure MySQL 数据库
- ruby-on-rails - FactoryBot:如何在 skip_create 工厂中使用瞬态来指定内部工厂中的属性?
- android - 带有颤振回调的报警服务
- javascript - 将 React 与其他库集成
- javascript - 带有在单击按钮后起作用的链接的 html 选择标签
- php - WP:通过 do_shortcode() 传递变量?
- login - 阻止管理员帐户通过 WooCommerce 登录页面登录
- c# - 将外部组用于树节点的 Lambda 表达式
- javascript - Bootstrap MODAL - 如何在没有流行音乐的情况下在移动设备中打开 YouTube?
- mysql - Django 不提交对 MySQL 数据库的更改