【问题标题】:Using beautifulsoup to extract text between the start of paragraph tag and a line break [closed]使用beautifulsoup提取段落标签开头和换行符之间的文本[关闭]
【发布时间】:2020-02-28 16:56:26
【问题描述】:

我有以下 HTML 文档

<p>
  "Year: 1932"
   <br>
   <br>
  "Total Share : 0.5 Lakhs (Pure Estimate)"
  <br>
  <br>
  "Verdict"
</p>

我目前正在使用 BeautifulSoup 来获取 HTML 中的其他元素,但我无法获得按原样获取这些行的方法。我将它们放在一行中。

【问题讨论】:

  • 你能举一个你想要的输出例子吗?
  • 年份:1932,总份额:05 万(纯估计),判决。这是我正在寻找的理想输出。
  • 我添加了一个解决方案,请检查一下。并且 response_data 应该有 HTML 文档

标签: html python-3.x beautifulsoup html-parsing


【解决方案1】:

这样试试

from bs4 import BeautifulSoup

response_data = <Your html tags>

soup_data = BeautifulSoup(response_data, features="html5lib")
string_data = soup_data.find('p').text.strip().replace("\n", ",").replace("\"", "").split(',')
data_list=[]
for strng in string_data:
    if strng.strip():
        data_list.append(strng.strip())

print(data_list)


【讨论】:

  • 我在尝试创建 beautifulsoup 对象时遇到错误@krishna。
  • FeatureNotFound: 找不到具有您要求的功能的树生成器:html5lib。需要安装解析器库吗?
  • 是的,你需要安装那个库。
  • 我们不能用 html.parser 库代替 html5lib 吗? @克里希纳
  • 你可以使用它。
【解决方案2】:

尝试像&lt;br/&gt;那样关闭br

【讨论】:

  • 实际上,提到的 html 是我试图使用 beautifulsoup 抓取的网站的一部分。我无法更改网页的结构。
猜你喜欢
  • 1970-01-01
  • 2011-07-13
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2011-09-09
相关资源
最近更新 更多