python - 如何在 Python 中使用 BeautifulSoup 在文本字符串后查找表格?

标签 python web-scraping beautifulsoup

我正在尝试从几个网页中提取数据,这些网页在显示表格的方式上并不统一。我需要编写代码来搜索文本字符串,然后立即转到该特定文本字符串之后的表。然后我想提取该表的内容。到目前为止,这是我得到的:

from BeautifulSoup import BeautifulSoup, SoupStrainer
import re

html = ['<html><body><p align="center"><b><font size="2">Table 1</font></b><table><tr><td>1. row 1, cell 1</td><td>1. row 1, cell 2</td></tr><tr><td>1. row 2, cell 1</td><td>1. row 2, cell 2</td></tr></table><p align="center"><b><font size="2">Table 2</font></b><table><tr><td>2. row 1, cell 1</td><td>2. row 1, cell 2</td></tr><tr><td>2. row 2, cell 1</td><td>2. row 2, cell 2</td></tr></table></html>']
soup = BeautifulSoup(''.join(html))
searchtext = re.compile('Table 1',re.IGNORECASE) # Also need to figure out how to ignore space
foundtext = soup.findAll('p',text=searchtext)
soupafter = foundtext.findAllNext()
table = soupafter.find('table') # find the next table after the search string is found
rows = table.findAll('tr')
for tr in rows:
    cols = tr.findAll('td')
    for td in cols:
        try:
            text = ''.join(td.find(text=True))
        except Exception:
            text = ""
        print text+"|",
print

但是,我收到以下错误:

    soupafter = foundtext.findAllNext()
AttributeError: 'ResultSet' object has no attribute 'findAllNext'

有使用 BeautifulSoup 的简单方法吗?

最佳答案

错误是由于findAllNextTag 对象的方法,但是 foundtext 是一个 ResultSet 对象,它是匹配标签的 list 或字符串。您可以遍历 foundtext 中的每个标签,但根据您的需要,使用 find 可能就足够了。 , 它只返回第一个匹配的标签。

这是您的代码的修改版本。将 foundtext 更改为使用 soup.find 后,我发现并修复了与 table 相同的问题。我将您的正则表达式修改为 ignore whitespace between the words :

from BeautifulSoup import BeautifulSoup, SoupStrainer
import re

html = ['<html><body><p align="center"><b><font size="2">Table 1</font></b><table><tr><td>1. row 1, cell 1</td><td>1. row 1, cell 2</td></tr><tr><td>1. row 2, cell 1</td><td>1. row 2, cell 2</td></tr></table><p align="center"><b><font size="2">Table 2</font></b><table><tr><td>2. row 1, cell 1</td><td>2. row 1, cell 2</td></tr><tr><td>2. row 2, cell 1</td><td>2. row 2, cell 2</td></tr></table></html>']
soup = BeautifulSoup(''.join(html))
searchtext = re.compile(r'Table\s+1',re.IGNORECASE)
foundtext = soup.find('p',text=searchtext) # Find the first <p> tag with the search text
table = foundtext.findNext('table') # Find the first <table> tag that follows it
rows = table.findAll('tr')
for tr in rows:
    cols = tr.findAll('td')
    for td in cols:
        try:
            text = ''.join(td.find(text=True))
        except Exception:
            text = ""
        print text+"|",
    print 

这个输出:

1. row 1, cell 1| 1. row 1, cell 2|
1. row 2, cell 1| 1. row 2, cell 2|

关于python - 如何在 Python 中使用 BeautifulSoup 在文本字符串后查找表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5711483/

相关文章:

python - 使用 beautiful soup 来解析给定 html 结构中的 href

python - 有 OrderedDict 理解吗?

python - 如何将 flask 配置为可通过公共(public) IP 接口(interface)访问?

python - 如何使用 PyCharm 运行文档测试?

python - 尝试从网站上抓取电子邮件地址

php - 使用 PHP 的 exec() 给出错误 : Fatal: [Errno 2] No such file or directory; did you install

r - 下载大文件时 httr GET 函数空间不足

Python BeautifulSoup 给 findAll 多个标签

c++ - 我的c++扩展程序与Faulthandler的行为不同

python - 格式化复数