嘗試在 pd.read_html 命令的表參考中傳遞一個變數。摘錄下面給出的代碼。有沒有辦法動態分配號碼?
這里要網頁中的第 6 個表。網頁上有多個編號為 0 到 15 的表格,需要將表格編號分配給一個變數。
import cloudscraper
import pandas as pd
url = 'sebi.gov.in/sebiweb/other/OtherAction.do?doPmr=yes'
yr = 2022
mth = 1
z=5
payload = { 'pmrId': 'INP000000043@@INP000000043@@ASK INVESTMENT MANAGERS PRIVATE LIMITED', 'year': yr, 'month': mth}
scraper = cloudscraper.CloudScraper() res = scraper.get(url, params=payload).text
df1 = pd.read_html(res)[z]
我得到的錯誤是: list indices must be integers or slices, not set
如果我這樣做df1 = pd.read_html(res)[5]了。
提前致謝..
uj5u.com熱心網友回復:
我不知道為什么你會得到z一個集合的錯誤。您可能想在print(z)之前添加一個列印陳述句以清楚地看到發生了什么。否則,代碼還有其他一些問題。
'sebi.gov.in/sebiweb/other/OtherAction.do?doPmr=yes'不是有效的 url 架構。你需要有https://- 這個請求是一個帖子,而不是一個獲取。這里的 params 引數不會被使用,因為它是一個帖子。
查看下面的編輯以查看您需要修復的內容:
import cloudscraper
import pandas as pd
url = 'https://sebi.gov.in/sebiweb/other/OtherAction.do?doPmr=yes'
yr = 2022
mth = 1
z=5
payload = { 'pmrId': 'INP000000043@@INP000000043@@ASK INVESTMENT MANAGERS PRIVATE LIMITED', 'year': yr, 'month': mth}
scraper = cloudscraper.CloudScraper()
res = scraper.post(url, data=payload).text
df1 = pd.read_html(res)[z]
輸出:
print(df1)
Investment Approach ... Assets under Management (AUM) as on last day of the month (Amount in INR crores)
Investment Approach ... Total
Unnamed: 0_level_2 ... Total
0 ASK Conviction Portfolio ... 187.47
1 ASK Domestic Resurgence Portfolio ... 1022.58
2 ASK Domestic Resurgence Portfolio STP ... 9.39
3 ASK Emerging Opportunities Portfolio ... 704.17
4 ASK Financial Opportunities Portfolio ... 1009.73
5 ASK Financial Opportunities Portfolio STP ... 7.00
6 ASK Growth Portfolio ... 2163.87
7 ASK Growth Portfolio STP ... 5.91
8 ASK High Conviction Portfolio ... 7.74
9 ASK Hybrid Portfolio ... 56.35
10 ASK India A Plus Portfolio ... 2.36
11 ASK India Select Portfolio ... 3848.58
12 ASK India Select Portfolio STP ... 14.31
13 ASK India Vision Portfolio ... 313.33
14 ASK India Vision Portfolio STP ... 7.18
15 ASK Indian Entrepreneur Portfolio ... 17441.59
16 ASK Indian Entrepreneur Portfolio STP ... 168.12
17 ASK Life Portfolio ... 149.47
18 ASK Liquid Strategy ... 335.53
19 ASK Specialised Portfolio ... 493.01
20 ASK Strategic Portfolio ... 82.57
21 Real Estate Special Opportunities Portfolio ... 166.58
22 Total ... 28196.85
[23 rows x 13 columns]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/448278.html
上一篇:使用scrapy清空結果檔案
