html - 用父元素的 beautifulsoup4 : does it affect the . 字符串解包元素?

标签 html web-scraping beautifulsoup python-3.4

我正在网络抓取如下表中的文本数据,我想获得结果:

Lorem ipsum
dolor sit amet
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    html = '''
<table>
<tr class="title last ">
  <td>
   Lorem ipsum
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
   <span class="caps">dolor
   </span>
   sit amet
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
   consectetur adipiscing elit,
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
   sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </td>
  <td>
  </td>
 </tr>
</table>
'''

我打开了 <span> beautifulsoup4 元素:

soup = BeautifulSoup(html)

# remove <span> tag but keep content
spans = soup.find_all('span')
for tag in spans:
    tag.unwrap()

但是,我想出了所有空的空行 <td>元素,或者 'dolor sit amet' 行不打印,即使我在使用 prettify 打印 html 时可以看到它。

# text with empty lines
for line in soup.find_all('td'):
    print(line.get_text().strip())
    print(line.string) # line with <span> prints None

# missing line <span>
for line in soup.find_all('td', text=re.compile(r'\w')):
    print(line.get_text().strip())

print(soup.prettify())

我做错了什么吗?我如何使用 unwrap() 并仍然访问所有没有空行的文本内容?

感谢您的帮助!

最佳答案

据我测试,您就在附近。应用 strip() 然后使用 re 模块将多个空格替换为一个空格,例如:

from bs4 import BeautifulSoup
import re

html = ''' 
<table>
<tr class="title last ">
  <td>
   Lorem ipsum
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
   <span class="caps">dolor
   </span>
   sit amet
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
   consectetur adipiscing elit,
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
   sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </td>
  <td>
  </td>
 </tr>
 <tr>
  <td>
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </td>
  <td>
  </td>
 </tr>
</table>
'''

soup = BeautifulSoup(html)

# remove <span> tag but keep content
spans = soup.find_all('span')
for tag in spans:
    tag.unwrap()

print('\n'.join(
  re.sub(r'\s+', ' ', td.text.strip()) 
    for td in soup.find_all('td') if td.text.strip()))

它产生:

Lorem ipsum
dolor sit amet
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

关于html - 用父元素的 beautifulsoup4 : does it affect the . 字符串解包元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28528594/

相关文章:

html - 修复水平滚动中的列

html - 设置 HTML5 Canvas 的高度和宽度并使内容在比例尺内

java - Java 运行时环境在运行 .\import.io 时检测到 fatal error

python - 我如何有条件地重试并重新抓取 Scrapy 中的当前页面?

python - 我的美丽汤刮刀无法按预期工作

python - 美汤刮痧 : Why won't the get_text method return the text of this element?

html - 我无法将样式属性高度应用于 div

javascript - 如何使用jquery替换html中的图像

search-engine - 网络爬行和网络抓取有什么区别?

python - 当我运行我的代码时,它返回 '[]' 。我怎样才能解决这个问题?