【发布时间】: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