Read_html 缺少第一个 table
Read_html missing first table
第一个 table 在竞选网站期间没有通过:
url = https://electproject.github.io/Early-Vote-2020G/GA_RO.html
代码如下:
import requests
from bs4 import BeautifulSoup
import pandas as pd
headers = {
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.99 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
}
url = r'https://electproject.github.io/Early-Vote-2020G/GA_RO.html'
r = requests.get(url, headers=headers).text
soup = BeautifulSoup(r, 'html.parser')
这两个都试过了,但仍然没有获得第一个 table 率 counties/votes/turnout
tables = soup.findAll('table')
dfs = list()
for table in tables:
df = pd.read_html(str(table))[0]
dfs.append(df)
其他尝试:
df = pd.read_html(r, flavor='html5lib')
两者都拉所有其他 table 但不是第一个。我认为这是由于 headers 具有排序功能,但不确定。
问题是第一个 table 是用 JavaScript 渲染的,HTML 中没有 <table>
table。
您可以直接从 JavaScript 获取数据(可以检查页面源代码以找到正确的 <script>
元素):
import json
data = soup.findAll('script', {
'data-for': 'htmlwidget-21712dd45dd736e3c1b9',
})[0].contents[0]
df = pd.DataFrame(json.loads(data)['x']['data']).T
输出:
0 1 2 3
0 APPLING 4453 12240 0.363807
1 ATKINSON 1431 4939 0.289735
2 BACON 3111 7071 0.439966
3 BAKER 872 2297 0.379626
4 BALDWIN 11850 27567 0.429862
.. ... ... ... ...
154 WHITFIELD 16980 57014 0.297822
155 WILCOX 1582 4838 0.326995
156 WILKES 2958 7204 0.410605
157 WILKINSON 2236 6761 0.33072
158 WORTH 4473 14601 0.306349
第一个 table 在竞选网站期间没有通过:
url = https://electproject.github.io/Early-Vote-2020G/GA_RO.html
代码如下:
import requests
from bs4 import BeautifulSoup
import pandas as pd
headers = {
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.99 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
}
url = r'https://electproject.github.io/Early-Vote-2020G/GA_RO.html'
r = requests.get(url, headers=headers).text
soup = BeautifulSoup(r, 'html.parser')
这两个都试过了,但仍然没有获得第一个 table 率 counties/votes/turnout
tables = soup.findAll('table')
dfs = list()
for table in tables:
df = pd.read_html(str(table))[0]
dfs.append(df)
其他尝试:
df = pd.read_html(r, flavor='html5lib')
两者都拉所有其他 table 但不是第一个。我认为这是由于 headers 具有排序功能,但不确定。
问题是第一个 table 是用 JavaScript 渲染的,HTML 中没有 <table>
table。
您可以直接从 JavaScript 获取数据(可以检查页面源代码以找到正确的 <script>
元素):
import json
data = soup.findAll('script', {
'data-for': 'htmlwidget-21712dd45dd736e3c1b9',
})[0].contents[0]
df = pd.DataFrame(json.loads(data)['x']['data']).T
输出:
0 1 2 3
0 APPLING 4453 12240 0.363807
1 ATKINSON 1431 4939 0.289735
2 BACON 3111 7071 0.439966
3 BAKER 872 2297 0.379626
4 BALDWIN 11850 27567 0.429862
.. ... ... ... ...
154 WHITFIELD 16980 57014 0.297822
155 WILCOX 1582 4838 0.326995
156 WILKES 2958 7204 0.410605
157 WILKINSON 2236 6761 0.33072
158 WORTH 4473 14601 0.306349