import re
from bs4 import BeautifulSoup
import requests
from ppprint import pprint
url = "https://www.countyofdane.com/clerk/elect2008d.html"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
is_table_row = False
tables = []
for line in soup.pre.get_text().splitlines():
# beginning of the table
if not is_table_row and " -" in line:
is_table_row = True
table = []
continue
# end of the table
if is_table_row and not line.strip():
is_table_row = False
tables.append(table)
continue
if is_table_row:
table.append(re.split("\s{2,}", line)) # splitting by 2 or more spaces
pprint(tables)
这个问题实际上发生在文本解析中,因为表位于
pre
元素内的纯文本。在你可以从这里开始。其思想是通过使用
-
头和表后面的空行来检测表的开头和结尾。大致如下:这将打印一个列表列表-每个表都有数据行的子列表:
^{pr2}$当然,这不包括表名和对角线标题,这可能很难获得,但并非不可能。另外,您可能需要将表中的合计行与其他数据行分开。无论如何,我认为这对你来说是一个很好的开始。在
相关问题 更多 >
编程相关推荐