如何在段落标记中使用其他标记来刮除段落文本中的文本?

2024-09-26 17:43:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个段落标签中搜集信息。

标记中还有一些其他标记。我将用下面的代码向您展示。你知道吗

这是你的名字

以下是要删除的html页面:

<div class="thecontent">
<p>Here&rsquo;s the schedule of matches for the weekend.</p>
<p>&nbsp;</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> &mdash;&nbsp;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> &mdash;&nbsp;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> &mdash;&nbsp;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> &mdash;&nbsp;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> &mdash;&nbsp;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> &mdash;&nbsp;Have enjoy it and celebrate it</p>
<p>&nbsp;</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> &mdash;&nbsp;Have enjoy it and celebrate it</p>
</p>
<p>&nbsp;</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())   

有了上面的信息,我只想提取团队之间的匹配和它发生的日期。下面的小信息是:

Saturday, August 17

Achara vs. timpao, — Have enjoy it and celebrate it

pritos vs. baola, — Have enjoy it and celebrate it

timpao vs. quadrsa, — Have enjoy it and celebrate it

Sunday, August 18

Achara vs. timpao, — Have enjoy it and celebrate it

pritos vs. qaudra, — Have enjoy it and celebrate it

timpao vs. Buad, — Have enjoy it and celebrate it

Monday, August 19

Achara vs. Buad, — Have enjoy it and celebrate it

我的意思是我不想要关于电视广播的信息(或者锚标签中的信息)。你知道吗


Tags: andhttpshttphavewwwitvshref
2条回答

不知道真正的来源是什么样的。例如,您可以删除标记并使用:has:not(:empty)作为目标。需要bs4.7.1+

from bs4 import BeautifulSoup as bs
import requests

r = requests.get('https://worldsoccertalk.com/2019/08/16/epl-commentator-assignments-nbc-sports-gameweek-2-3/')
soup = bs(r.content, 'lxml')

for a in soup("a"):
    a.decompose()

for i in soup.select('.thecontent p:has(strong:not(:contains("SEE MORE"))), .thecontent p:has(strong:not(:contains("SEE MORE"))) ~ p:not(:empty)'):
    data = i.text.strip()
    if data:
        if ' vs. ' in data:
            items = data.split(',')
            print(', '.join([items[0], items[-1].split('—')[1]]))
        else:
            print(data)

enter image description here

看起来包含内容的段落还包含提示“,-享受它并庆祝它”,因此当您检索其内容时,它总是添加。你能做的就是通过做一些类似的事情来去除绳子的尾部

if len(pta[i] > 33):
  pta[i].get_text()[:-33]

这样您将删除结果字符串的最后33个字符。你知道吗

相关问题 更多 >

    热门问题