使用 pandas 和 bs4 解析复杂的 multi-header html table
Parse complex multi-header html table with pandas and bs4
我已经使用 bs4
、pandas
和 lxml
库来解析上面的 html table,但我没有成功。使用 pandas
我尝试跳过行并将 header 设置为 0 但是结果是 DataFrame
高度非结构化并且似乎还缺少一些数据。
对于其他 2 个库,我尝试使用选择器,甚至使用 tbody
部分中的 xpath
,但在这两种情况下我都收到一个空列表。
这就是我要检索的内容:
任何人都可以帮助我了解如何抓取该数据吗?
谢谢!
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
page = urlopen('https://transparency.entsoe.eu/generation/r2/actualGenerationPerProductionType/show?name=&defaultValue=true&viewType=TABLE&areaType=BZN&atch=false&datepicker-day-offset-select-dv-date-from_input=D&dateTime.dateTime=09.08.2017%2000:00%7CUTC%7CDAYTIMERANGE&dateTime.endDateTime=09.08.2017%2000:00%7CUTC%7CDAYTIMERANGE&area.values=CTY%7C10YES-REE------0!BZN%7C10YES-REE------0&productionType.values=B01&productionType.values=B02&productionType.values=B03&productionType.values=B04&productionType.values=B05&productionType.values=B06&productionType.values=B07&productionType.values=B08&productionType.values=B09&productionType.values=B10&productionType.values=B11&productionType.values=B12&productionType.values=B13&productionType.values=B14&productionType.values=B20&productionType.values=B15&productionType.values=B16&productionType.values=B17&productionType.values=B18&productionType.values=B19&dateTime.timezone=UTC&dateTime.timezone_input=UTC')
soup = BeautifulSoup(page.read())
table = soup.find('tbody')
res = []
row = []
for tr in table.find_all('tr'):
for td in tr.find_all('td'):
row.append(td.text)
res.append(row)
row = []
df = pd.DataFrame(data=res)
然后使用 df.columns
添加列名称并删除空列。
编辑: 建议修改后的 for 循环。 (比尔贝尔)
>>> for tr in table.find_all('tr'):
... for td in tr.find_all('td'):
... row.append(td.text.strip())
... res.append(row)
... row = []
for
语句的原始形式编译失败。
-
append
的原始形式在常量中留下换行符和空格。
我已经使用 bs4
、pandas
和 lxml
库来解析上面的 html table,但我没有成功。使用 pandas
我尝试跳过行并将 header 设置为 0 但是结果是 DataFrame
高度非结构化并且似乎还缺少一些数据。
对于其他 2 个库,我尝试使用选择器,甚至使用 tbody
部分中的 xpath
,但在这两种情况下我都收到一个空列表。
这就是我要检索的内容:
任何人都可以帮助我了解如何抓取该数据吗? 谢谢!
from bs4 import BeautifulSoup
from urllib.request import urlopen
import pandas as pd
page = urlopen('https://transparency.entsoe.eu/generation/r2/actualGenerationPerProductionType/show?name=&defaultValue=true&viewType=TABLE&areaType=BZN&atch=false&datepicker-day-offset-select-dv-date-from_input=D&dateTime.dateTime=09.08.2017%2000:00%7CUTC%7CDAYTIMERANGE&dateTime.endDateTime=09.08.2017%2000:00%7CUTC%7CDAYTIMERANGE&area.values=CTY%7C10YES-REE------0!BZN%7C10YES-REE------0&productionType.values=B01&productionType.values=B02&productionType.values=B03&productionType.values=B04&productionType.values=B05&productionType.values=B06&productionType.values=B07&productionType.values=B08&productionType.values=B09&productionType.values=B10&productionType.values=B11&productionType.values=B12&productionType.values=B13&productionType.values=B14&productionType.values=B20&productionType.values=B15&productionType.values=B16&productionType.values=B17&productionType.values=B18&productionType.values=B19&dateTime.timezone=UTC&dateTime.timezone_input=UTC')
soup = BeautifulSoup(page.read())
table = soup.find('tbody')
res = []
row = []
for tr in table.find_all('tr'):
for td in tr.find_all('td'):
row.append(td.text)
res.append(row)
row = []
df = pd.DataFrame(data=res)
然后使用 df.columns
添加列名称并删除空列。
编辑: 建议修改后的 for 循环。 (比尔贝尔)
>>> for tr in table.find_all('tr'):
... for td in tr.find_all('td'):
... row.append(td.text.strip())
... res.append(row)
... row = []
for
语句的原始形式编译失败。-
append
的原始形式在常量中留下换行符和空格。