BeautifulSoup 如何提取 <br> 标签后的文本
BeautifulSoup how to extract text after <br> tag
我不知道如何使用 BeautifulSoup 到达下一段以及如何提取我想要的特定文本。因为我是 Python 和 BS4 的新手。
我的 HTML 如下:
<div class="inner-content">
<div class="bred"></div>
<div class="clrbth"></div>
<h1></h1>
<h4></h4>
...
...
...
<p></p>
<p></p>
<p>
<!--This text I don't want -->
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<br></br>
<!-- The text I want to extract using BeautifulSoup-->
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
<p></p>
<p></p>
...
...
...
<div class="bred"></div>
<div class="clrbth"></div>
<h1></h1>
</div>
请告诉我如何从我的 HTML 中提取上述文本。谢谢
您可以使用find_all()
method and the limit
argument to get the third p
tag in your html. Next use the .find
which return the first br
tag in the third paragraph. From there you can use the .next_siblings
method which return a generator object and the .join
函数。
>>> third_p = soup.find_all('p', limit=3)[-1]
>>> ''.join(third_p.find('br').next_siblings)
我不知道如何使用 BeautifulSoup 到达下一段以及如何提取我想要的特定文本。因为我是 Python 和 BS4 的新手。
我的 HTML 如下:
<div class="inner-content">
<div class="bred"></div>
<div class="clrbth"></div>
<h1></h1>
<h4></h4>
...
...
...
<p></p>
<p></p>
<p>
<!--This text I don't want -->
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<br></br>
<!-- The text I want to extract using BeautifulSoup-->
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
<p></p>
<p></p>
...
...
...
<div class="bred"></div>
<div class="clrbth"></div>
<h1></h1>
</div>
请告诉我如何从我的 HTML 中提取上述文本。谢谢
您可以使用find_all()
method and the limit
argument to get the third p
tag in your html. Next use the .find
which return the first br
tag in the third paragraph. From there you can use the .next_siblings
method which return a generator object and the .join
函数。
>>> third_p = soup.find_all('p', limit=3)[-1]
>>> ''.join(third_p.find('br').next_siblings)