我正在尝试获取标有“关键费率”的表中的数据。但是,我只能提取名称,因为它们具有独特的样式 ( )。
我想要表中的数据并使用 Pandas 排列到表中。 请帮忙
代码:
URL = "https://www.centralbank.go.ke/"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
job_elems = soup.find_all('td', class_="tg-4eph")
for job_elem in job_elems:
title_elem = job_elem.find('small')
if title_elem:
print(title_elem.text.strip())
解决此问题的一种方法是查找具有与第一个元素关联的字符串“Key Rates”的表。
类似这样的:
import requests
from bs4 import BeautifulSoup as BS
import pandas as pd
import io
with requests.get("https://www.centralbank.go.ke") as response:
response.raise_for_status()
soup = BS(response.text, "lxml")
for table in soup.select("table.tg"):
if (th := table.select_one("th")) and th.get_text() == "Key Rates":
html = io.StringIO(str(table))
df = pd.read_html(html)[0]
print(df.to_string(index=False))
break
输出:
Key Rates Key Rates.1 Key Rates.2 Key Rates.3
Central Bank Rate Central Bank Rate 13.00% 05/06/2024
Inter-Bank Rate Inter-Bank Rate 13.26% 04/07/2024
CBK Discount Window CBK Discount Window 16.00% 05/06/2024
91-Day T-Bill 91-Day T-Bill 15.977% 01/07/2024
REPO REPO 0.00% 08/05/2024
Inflation Rate Inflation Rate 4.64% June,2024
Lending Rate Lending Rate 16.45% April,2024
Savings Rate Savings Rate 4.14% April,2024
Deposit Rate Deposit Rate 10.77% April,2024
KBRR KBRR 8.9% 27/06/2016