我正在抓取網站tickertapeenter link description here,以提取有關產品的資訊。 決議網站后的預期結果。
我面臨的問題,div類資訊是非常動態的
<div data-section-tag="key-metrics" class="jsx-382396230 ratios-card sp-card"><h2 class="jsx-382396230"><span class="jsx-382396230 content"> Key Metrics< /span></h2><div class="jsx-382396230 stats"><div class="jsx-1785027547 statbox">< div><div class="title font-medium text-dark text-14 pointer">。 <span class="jsx-559150734 key-ratio-title relative"><span class="jsx-559150734 ellipsis desktop--only"> Realtime NAV</span><span class="jsx-559150734 ellipsis mob-only"> Realtime NAV</span><div class="jsx-324047672 tooltip-root arrow-bottom arrow-left content-top content-left font-regular text-13 lh-138" style="color: rgb(255, 255, 255);"><h4 class="jsx-559150734 tooltip-head mb4 font-medium">realtime NAV</h4><p class="jsx-559150734 lh-138">。 每只股票在基礎資產和現金中的部分價值</p></div></span></div><div class="value text-15 ellipsis">? 181。 73</div></div><div><div class="title font-medium text-dark text-14 pointer"><span class="jsx-559150734 key-ratio-title relative"><span class="jsx-559150734 ellipsis desktop-only"> AUM<。 /span><span class="jsx-559150734 ellipsis text-center mob-only">AUM</span><div class="jsx-324047672 tooltip-root arrow-bottom arrow-middle content-top content-middle font-regular text-13 lh-138" style="color: rgb(255, 255, 255);"><h4 class="jsx-559150734 tooltip-head mb4 font-medium"> AUM</h4><p class="jsx-559150734 lh-138">。 資產管理公司管理的基金總市值</p></div></span></div><div class="value text-15 ellipsis">? 1,335。 35cr</div></div><div><div class="title font-medium text-dark text-14 pointer"><span class="jsx-559150734 key-ratio-title relative"><span class="jsx-559150734 ellipsis desktop-only">。 支出比率</span><span class="jsx-559150734 ellipsis text-right mob-only">支出比率</span><div class="jsx-324047672 tooltip-root font-regular text-13 lh-138" style="color: rgb(255, 255, 255);"><h4 class="jsx-559150734 tooltip-head mb4 font-medium">Expense Ratio</h4> <p class="jsx-559150734 lh-138"> 以基金資產的百分比衡量的經營和管理成本</p></div></span></div><div class="value text-15 ellipsis">0。 12%</div></div><div><div class="title font-medium text-dark text-14 pointer" ><span class="jsx-559150734 key-ratio-title relative" > <span class="jsx-559150734 ellipsis desktop--only"> Category Exp Ratio</span><span class="jsx-559150734 ellipsis mob-only">Cat. 支出比率。 </span><div class="jsx-324047672 tooltip-root arrow-bottom arrow-left content-top content-left font-regular text-13 lh-138" style="color: rgb(255, 255, 255);"><h4 class="jsx-559150734 tooltip-head mb4 font-medium">類別支出比例</h4><p class="jsx-559150734 lh-138">。 以基金資產的百分比衡量的運行同一行業ETF的平均運營和管理成本</p></div></span></div><div class="value text-15 ellipsis">0。 22%</div></div><div><div class="title font-medium text-dark text-14 pointer"><span class="jsx-559150734 key-ratio-title relative"><span class="jsx-559150734 ellipsis desktop-only">。 追蹤錯誤</span><span class="jsx-559150734 ellipsis text-center mob-only">追蹤錯誤</span><div class="jsx-324047672 tooltip-root font-regular text-13 lh-138" style="color: rgb(255, 255, 255);"><h4 class="jsx-559150734 tooltip-head mb4 font-medium">跟蹤錯誤</h4><p class="jsx-559150734 lh-138"> 證券的表現與它所追蹤的基準指數之間的差異</p></div></span></div><div class="value text-15 ellipsis">0。 08%</div></div><div><div class="title font-medium text-dark text-14 pointer" ><span class="jsx-559150734 key-ratio-title relative" > <span class="jsx-559150734 ellipsis desktop--only">Category Tracking Err</span><span class="jsx-559150734 ellipsis text-right mob-only">Cat. 追蹤Err. </span><div class="jsx-324047672 tooltip-root font-regular text-13 lh-138" style="color: rgb(255, 255, 255);"><h4 class="jsx-559150734 tooltip-head mb4 font-medium">類別跟蹤Err</h4><p class="jsx-559150734 lh-138">。 ETF同業表現與它所追蹤的基準指數之間的平均差異</p></div></span></div><div class="value text-15 ellipsis">0。 27%</div></div></div></div></div>/code>
我開發的代碼用于提取資訊
from bs4 import BeautifulSoup as bs
s=requests.Session()
response=s.get('https://www.tickertape.in/etfs/kotak-nifty-50-etf-KOTK')
soup = bs(response.text,'html.parser')
res=soup.find("div",{"data-section-tag":"key-metrics"}).get_text()。
#獲得AUM值。
#AUM_location被加了7,因為AUM是重復的,想洗掉符號?。
print("The AUM value",res[res. find('AUM') ((len('AUM')*2) 1) : res. find('Expense Ratio') ])
#To get the Expense ratio[/span].
print("The Expense ratio",res[res. find('Expense Ratio') (len('Expense Ratio')*2) : res. find('Sector Expense') ])
#To get the tracking error[/span]#。
print("The Tracking Error",res[res. find('Tracking Error') (len('Tracking Error')*2):res. find('Sector Tracking Error') ])
#關閉連接。
s.close()
目前,我正在提取文本,并根據長度分割陣列
是否有更好的方法?
是否有更好的方法來提取資訊?
uj5u.com熱心網友回復:
我會在一個script標簽中提取容納所有頁面資料的JS物件,并使用json包進行決議,然后提取你想要的值:
import re, json, requests
response = requests.get('https://www.tickertape.in/etfs/kotak-nifty-50-etf-KOTK')
data = json.load(re.search(r'({"props".*})'/span>, response.text).group(1)
ratios = data['props']['pageProps']['securityInfo'] ['ratios']
print("AUM值", '{:. 2f}'.format(ratios['asstUnderMan'])
print("The Expense ratio", '{:.2%}'. format(ratios['expenseRatio']/100)
print("The Tracking Error", '{: .2%}'. format(ratios['trackErr'/span>]/100)
uj5u.com熱心網友回復:
我得到了想要的輸出。我只用scrapy來應用xpath的目的。因為xpath可以幫助我輕松地抓取資料。
代碼:
import scrapy
class Ticker(scrapy.Spider)。
name = 'ticker'"https://www.tickertape.in/etfs/kotak-nifty-50-etf-KOTK"]
def parse(self, response):
產生 {
'Realtime NAV': response.xpath('(//div[@class="value text-15 ellipsis"])[1]/text()')。get() 。
'AUM': response.xpath('(//div[@class="value text-15 ellipsis"])[2]/text()'/span>).get()。
'Expense Ratio': response.xpath('(//div[@class="value text-15 ellipsis"])[3]/text()').get()。
'Sctr Expense Ratio': 回應。 xpath('(//div[@class="value text-15 ellipsis"])[4]/text()')。get()。
'Tracking Error': response.xpath('(//div[@class="value text-15 ellipsis"]) [5]/text()').get()。
'Sctr Tracking Error': response.xpath('(//div[@class="value text-15 ellipsis"] ) [6]/text()').get()
}
Scrapy中的輸出:
{'Realtime NAV': '? 181.56', 'AUM': '? 1,463.42cr', '費用比率': '0.12%'/span>, 'Sctr Expense Ratio'/span>: '0.22%', '跟蹤誤差': '0.08%', 'Sctr Tracking Error': '0.26%'}。
在csv中輸出:
Realtime NAV AUM Expense Ratio Sctr Expense Ratio Tracking Error Sctr Tracking Error。
? 181.56 ? 1, 463.42cr 0. 12% 0.22% 0.08% 0.26%。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/333267.html
標籤:
