我正在嘗試抓取此站點 URL:https ://statusinvest.com.br/fundos-imobiliarios/urpr11 ,以從表格中獲取此特定 REIT 的股息資訊(稍后我將對此進行概括)。這是包含資訊的表:
股息表
我能夠從表中獲取日期和值,但僅限于第一頁。當我更改表格頁面時,網站 URL 沒有任何修改,所以我實際上不知道如何處理這個問題。任何幫助,將不勝感激。
Obs:如果解決方法不依賴于頁面數量,那就太好了,因為一些 REIT 可以擁有超過 2 頁的資訊。
這就是我目前從第一頁獲取資訊的方式
from bs4 import BeautifulSoup
import requests
URL = "https://statusinvest.com.br/fundos-imobiliarios/urpr11"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
test = soup.find_all("tr", class_="")
rows = []
for r in test:
if not r.find("td", title="Rendimento"):
continue
row = []
for child in r.findChildren():
if child.text.lower()=="rendimento":
continue
print(child.text)
row.append(child.text)
rows.append(row)
uj5u.com熱心網友回復:
內容是由 動態提供的JavaScript,requests本質上不是渲染的,因此您不會以這種方式獲取所有資料。
怎么修?
您可以selenium像人類在瀏覽器中那樣與網站進行互動 - 用于稍后和更復雜的問題。
但在這種情況下,它要簡單得多,不需要selenium. 只需獲取用于提供表格的JSON資料:JavaScript
data = json.loads(soup.select_one('#results')['value'])
將其轉換DataFrame為根據您的需要進行調整并將其保存為 csv、json、....
pd.DataFrame(data).to_csv('yourFile.csv', index=False)
網站上顯示的列更多,請查看示例的輸出。這些調整將通過僅讀取特定資料和重命名列標題來為您提供預期的調整:
df = pd.DataFrame(data, columns=['et','ed', 'pd', 'v'])
df.columns = ['TIPO','DATA COM','PAGAMENTO','VALOR']
df.to_csv('yourFile.csv', index=False)
| 知識產權局 | 資料通訊 | 帕加門托 | 勇氣 |
|---|---|---|---|
| 倫迪門托 | 25/02/2022 | 15/03/2022 | 1.635 |
| 倫迪門托 | 2022 年 1 月 31 日 | 14/02/2022 | 1.63 |
例子
from bs4 import BeautifulSoup
import requests, json
import pandas as pd
URL = "https://statusinvest.com.br/fundos-imobiliarios/urpr11"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
data = json.loads(soup.select_one('#results')['value'])
pd.DataFrame(data)
#or with adjustment as mentioned above
#df = pd.DataFrame(data, columns=['et','ed', 'pd', 'v'])
#df.columns = ['TIPO','DATA COM','PAGAMENTO','VALOR']
#df.to_csv('yourFile.csv', index=False)
輸出
| 是的 | 米 | d | 廣告 | 編 | PD | 等 | 等 | v | ov | sv | 索夫 | 形容詞 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 25/02/2022 | 15/03/2022 | 倫迪門托 | 倫迪門托 | 1.635 | 1,63500000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2022 年 1 月 31 日 | 14/02/2022 | 倫迪門托 | 倫迪門托 | 1.63 | 1,63000000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 12 月 30 日 | 14/01/2022 | 倫迪門托 | 倫迪門托 | 1.67 | 1,67000000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 11 月 30 日 | 2021 年 14 月 12 日 | 倫迪門托 | 倫迪門托 | 1.869 | 1,86900000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 10 月 29 日 | 2021 年 11 月 16 日 | 倫迪門托 | 倫迪門托 | 1.37 | 1,37000000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 9 月 30 日 | 15/10/2021 | 倫迪門托 | 倫迪門托 | 2.17 | 2,17000000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 8 月 31 日 | 2021 年 9 月 15 日 | 倫迪門托 | 倫迪門托 | 2.01 | 2,01000000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 7 月 30 日 | 13/08/2021 | 倫迪門托 | 倫迪門托 | 1.48 | 1,48000000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 6 月 30 日 | 2021 年 7 月 14 日 | 倫迪門托 | 倫迪門托 | 2.4 | 2,40000000 | - | 錯誤的 | ||
| 0 | 0 | 0 | 2021 年 5 月 31 日 | 15/06/2021 | 倫迪門托 | 倫迪門托 | 2.06 | 2,06000000 | - | False | ||
| 0 | 0 | 0 | 30/04/2021 | 14/05/2021 | Rendimento | Rendimento | 1.185 | 1,18500000 | - | False | ||
| 0 | 0 | 0 | 31/03/2021 | 15/04/2021 | Rendimento | Rendimento | 2.87 | 2,87000000 | - | False | ||
| 0 | 0 | 0 | 26/02/2021 | 12/03/2021 | Rendimento | Rendimento | 2.09 | 2,09000000 | - | False | ||
| 0 | 0 | 0 | 29/01/2021 | 12/02/2021 | Rendimento | Rendimento | 2.25 | 2,25000000 | - | False | ||
| 0 | 0 | 0 | 30/12/2020 | 15/01/2021 | Rendimento | Rendimento | 2.01 | 2,01000000 | - | False | ||
| 0 | 0 | 0 | 30/11/2020 | 14/12/2020 | Rendimento | Rendimento | 2.03668 | 2,03668260 | - | False | ||
| 0 | 0 | 0 | 30/10/2020 | 13/11/2020 | Rendimento | Rendimento | 3.24 | 3,24000000 | - | False | ||
| 0 | 0 | 0 | 30/09/2020 | 15/10/2020 | Rendimento | Rendimento | 2.15 | 2,15000000 | - | False | ||
| 0 | 0 | 0 | 31/08/2020 | 15/09/2020 | Rendimento | Rendimento | 1.35 | 1,35000000 | - | False | ||
| 0 | 0 | 0 | 31/07/2020 | 14/08/2020 | Rendimento | Rendimento | 0.814098 | 0,81409811 | - | False | ||
| 0 | 0 | 0 | 30/06/2020 | 15/07/2020 | Rendimento | Rendimento | 1.56063 | 1,56063128 | - | False | ||
| 0 | 0 | 0 | 29/05/2020 | 15/06/2020 | Rendimento | Rendimento | 0.778074 | 0,77807445 | - | False | ||
| 0 | 0 | 0 | 30/04/2020 | 11/05/2020 | Rendimento | Rendimento | 0.615445 | 0,61544523 | - | False | ||
| 0 | 0 | 0 | 14/04/2020 | 15/04/2020 | Rendimento | Rendimento | 0.189474 | 0,18947368 | - | False |
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/448272.html
