我对网络抓取完全陌生,有人能告诉我如何从这个网站上抓取尾随回报(%(表吗-https://www.valueresearchonline.com/funds/newsnapshot.asp?schemecode=16854以隐身模式打开它,并以显示的相同方式将其保存到excel中。
谢谢。
import requests
from bs4 import BeautifulSoup
import pandas as pd
res = requests.get("https://www.valueresearchonline.com/funds/16854/franklin-india-ultra-short-bond-fund-super-institutional-plan-direct-plan/")
soup = BeautifulSoup(res.text, "html.parser")
table = soup.find("table",{"id":"trailing-returns-table"})
columns = [i.get_text(strip=True) for i in table.find_all("th")]
data = []
for tr in table.find("tbody").find_all("tr"):
data.append([td.get_text(strip=True) for td in tr.find_all("td")])
df = pd.DataFrame(data, columns=columns)
df.to_excel("data.xlsx", index=False)
输出将保存到excel文件中。
输出:
YTD 1-Day 1-W 1-M 3-M 6-M 1-Y 3-Y 5-Y 7-Y 10-Y
0 Fund 0.76 0.07 0.20 0.90 2.48 4.59 4.60 7.36 8.24 8.85 --
1 CCIL T Bill Liquidity Weight 2.12 -0.01 0.03 0.14 0.68 1.86 3.74 4.09 4.29 4.79 --
2 Debt: Ultra Short Duration 3.85 0.02 0.06 0.53 1.88 3.36 6.98 6.55 7.19 8.18 --
Pandas更容易保存到excel文件或csv文件中,也更容易分析数据。否则,使用熊猫内部处理的openpyxl或xlsxwriter编写excel需要一些工作