如何解析这个?尝试使用 BeautifulSoup 和 Python 从非 HTML 网页提取数据
How to parse this? Trying to pull data from non-HTML webpage using BeautifulSoup and Python
BeautifulSoup & HTML新手,没见过这种类型的页面。我正在尝试从威斯康星州戴恩县 2008 年的总统竞选中提取数据。
Link: https://www.countyofdane.com/clerk/elect2008d.html
总统竞选的数据似乎是硬编码的table?它没有存储在 HTML 标签之间,也没有存储在我之前遇到的任何东西中。
我可以通过某种方式遍历 < !-- #-->
来提取数据吗?我是否应该将页面保存为 HTML 文件并在 table 周围添加 body 标记以便更容易解析?
这个问题实际上是文本解析,因为table在纯文本中pre
元素。
您可以从这里开始。这个想法是通过使用 -----
headers 和 table 之后的空行来检测 table 的开始和结束。沿着这些线的东西:
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)
这将打印一个列表列表 - 一个包含每个 table 数据行的子列表:
[
[
['0001 T ALBION WDS 1-2', '753', '315', '2', '4', '1', '0', '5', '2', '0', '1'],
['0002 T BERRY WDS 1-2', '478', '276', '0', '0', '0', '0', '2', '0', '0', '1'],
...
['', 'CANDIDATE TOTALS', '205984', '73065', '435', '983', '103', '20', '1491', '316', '31', '511'],
['', 'CANDIDATE PERCENT', '72.80', '25.82', '.15', '.34', '.03', '.52', '.11', '.01', '.18']],
[
['0001 T ALBION WDS 1-2', '726', '323', '0'],
['0002 T BERRY WDS 1-2', '457', '290', '1'],
['0003 T BLACK EARTH', '180', '107', '0'],
...
],
...
]
当然,这不包括 table 名称和对角线 headers,这可能很难获得,但并非不可能。另外,您可能希望将总计行与 table 的其他数据行分开。无论如何,我认为这对您来说是一个很好的开始示例。
BeautifulSoup & HTML新手,没见过这种类型的页面。我正在尝试从威斯康星州戴恩县 2008 年的总统竞选中提取数据。
Link: https://www.countyofdane.com/clerk/elect2008d.html
总统竞选的数据似乎是硬编码的table?它没有存储在 HTML 标签之间,也没有存储在我之前遇到的任何东西中。
我可以通过某种方式遍历 < !-- #-->
来提取数据吗?我是否应该将页面保存为 HTML 文件并在 table 周围添加 body 标记以便更容易解析?
这个问题实际上是文本解析,因为table在纯文本中pre
元素。
您可以从这里开始。这个想法是通过使用 -----
headers 和 table 之后的空行来检测 table 的开始和结束。沿着这些线的东西:
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)
这将打印一个列表列表 - 一个包含每个 table 数据行的子列表:
[
[
['0001 T ALBION WDS 1-2', '753', '315', '2', '4', '1', '0', '5', '2', '0', '1'],
['0002 T BERRY WDS 1-2', '478', '276', '0', '0', '0', '0', '2', '0', '0', '1'],
...
['', 'CANDIDATE TOTALS', '205984', '73065', '435', '983', '103', '20', '1491', '316', '31', '511'],
['', 'CANDIDATE PERCENT', '72.80', '25.82', '.15', '.34', '.03', '.52', '.11', '.01', '.18']],
[
['0001 T ALBION WDS 1-2', '726', '323', '0'],
['0002 T BERRY WDS 1-2', '457', '290', '1'],
['0003 T BLACK EARTH', '180', '107', '0'],
...
],
...
]
当然,这不包括 table 名称和对角线 headers,这可能很难获得,但并非不可能。另外,您可能希望将总计行与 table 的其他数据行分开。无论如何,我认为这对您来说是一个很好的开始示例。