【问题标题】:BeautifulSoup missing/skipping tagsBeautifulSoup 缺少/跳过标签
【发布时间】:2017-09-06 09:34:31
【问题描述】:

如果您能指出我正确的方向,将不胜感激。有没有更好的方法来执行此操作并捕获所有数据(使用 html 标签类“文档文本”))...

如果我喜欢这样。我在最后的原始 html 字符串中缺少一些标签,大小为 20K(所以它有很多数据)。

soup = BeautifulSoup(r.content, 'html5lib')
c.case_html = str(soup.find('div', class_='DocumentText')
print(self.case_html)

以下是用于抓取的代码,目前可以正常工作,但添加第二个新标签时它已损坏。

 soup = BeautifulSoup(r.content, 'html5lib')
 c.case_html = str(soup.find('div', class_='DocumentText').find_all(['p','center','small']))
 print(self.case_html)

示例html如下,原来是20K左右的字符串大小

<form name="form1" id="form1">
<div id="theDocument" class="DocumentText" style="position: relative; float: left; overflow: scroll; height: 739px;">
<p>PTag</p>
<p> <center> First center </center> </p>
<small> this is small</small>
<p>...</p>
<p> <center> Second Center </center> </p>
<p>....</p>
</div>
</form>

预期的输出是这样的

<div id="theDocument" class="DocumentText" style="position: relative; float: left; overflow: scroll; height: 739px;">
<p>PTag</p>
<p> <center> First center </center> </p>
<small> this is small</small>
<p>...</p>
<p> <center> Second Center </center> </p>
<p>....</p>
</div>

【问题讨论】:

  • c.case_html = str(soup.find('div', class_='DocumentText') 你为什么要把它转换成string
  • 你想从上面粘贴的元素中解析什么文本?
  • 你的预期输出是什么?
  • 预期输出为
    之间的所有内容

标签: python web-scraping beautifulsoup


【解决方案1】:

你可以试试这个。我只是根据您给定的 html 代码来回答。如果您需要澄清,请告诉我。谢谢!

 soup = BeautifulSoup(r.content, 'html5lib')
 case_html = soup.select('div.DocumentText')
 print(case_html.get_text())

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2017-09-03
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多