亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從股票網站頁面提取特定的字符串匹配

從股票網站頁面提取特定的字符串匹配

臨摹微笑 2023-12-26 15:24:10
我正在嘗試使用下面的代碼來抓取股票市值。起初我傳統上嘗試獲取使用 bs4 的列表market cap values。當我以前 print(x.find('span',{'class': 'Trsdu(0.3s)'}).text)這樣做時,我遇到了AttributeError: 'NoneType' object has no attribute 'text'錯誤。  for x in marketCapArray:        print(x.find('span',{'class': 'Trsdu(0.3s)'}).text)我不知道如何解決特定于我的代碼的上述錯誤。因此,我采取了另一種方法,使用正則表達式來簡單地提取所需的值,并在下面進行了嘗試。主要代碼import bs4import reimport requestsfrom bs4 import BeautifulSoupfrom urllib.request import urlopendef pickTopGainers():  url =  'https://in.finance.yahoo.com/gainers?offset=0&count=100'  page = urlopen(url)  soup = bs4.BeautifulSoup(page,"html.parser")  marketCapArray = soup.find_all('td', {'class': 'Va(m) Ta(end) Pstart(20px) Pend(10px) W(120px) Fz(s)', 'aria-label': 'Market cap'})  print(str(marketCapArray))  xi = re.findall("........</span>", str(marketCapArray)) # regex-use-1  pi = re.sub("(</span>|....>N/A|>|\")","", str(xi))  print(pi)pickTopGainers()結果這就是print(str(marketCapArray)會輸出的內容。(只粘貼了一部分)[<td aria-label="Market cap" class="Va(m) Ta(end) Pstart(20px) Pend(10px) W(120px) Fz(s)" colspan="" data-reactid="93"><span class="Trsdu(0.3s)" data-reactid="94">159.404M</span></td>, <td aria-label="Market cap" class="Va(m) Ta(end) Pstart(20px) Pend(10px) W(120px) Fz(s)" colspan="" data-reactid="119"><span class="Trsdu(0.3s)" data-reactid="120">533.97M</span></td>, <td aria-label="Market cap" class="Va(m) Ta(end) Pstart(20px) Pend(10px) W(120px) Fz(s)" colspan="" data-reactid="145"><span data-reactid="146">N/A</span></td>, <td aria-label="Market cap" class="Va(m) Ta(end) Pstart(20px) Pend(10px) W(120px) Fz(s)" colspan="" data-reactid="171"><span class="Trsdu(0.3s)" data-reactid="172">2.952B</span></td>, <td aria-label="Market cap" class="Va(m) Ta(end) Pstart(20px) Pend(10px) W(120px) Fz(s)" colspan="" data-reactid="197"><span class="Trsdu(0.3s)" data-reactid="198">9.223B</span></td>, 這是 的輸出print(pi)。也是最終的輸出。['159.404M', '533.97M', '', '2.952B', '9.223B', '']問題如何避免在上面使用正則表達式替換(re.sub)Main Code來實現給定的最終輸出pi?或者建議我正確的方法來做到這一點。我覺得我的正則表達式令人不快。
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

<table>您可以在存儲所有信息的 內逐行迭代。例如:


import requests

from bs4 import BeautifulSoup



url = 'https://in.finance.yahoo.com/gainers?offset=0&count=100'


soup = BeautifulSoup(requests.get(url).content, 'html.parser')


fmt_string = '{:<15} {:<60} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10} {:<10}'

print(fmt_string.format('Symbol', 'Name', 'Price(int)', 'Change', '% change', 'Volume', 'AvgVol(3M)', 'Market Cap', 'PE ratio'))

for row in soup.select('table:has(a[href*="/quote/"]) > tbody > tr'):

    cells = [td.get_text(strip=True) for td in row.select('td')]

    print(fmt_string.format(*cells[:-1]))

印刷:


Symbol          Name                                                         Price(int) Change     % change   Volume     AvgVol(3M) Market Cap PE ratio  

CCCL.NS         Consolidated Construction Consortium Limited                 0.2000     +0.0500    +33.33%    57,902     290,154    159.404M   N/A       

KSERASERA.NS    KSS Limited                                                  0.2500     +0.0500    +25.00%    1.607M     2.601M     533.97M    N/A       

BONLON.BO       BONLON INDUSTRIES LIMITED                                    21.60      +3.60      +20.00%    16,000     N/A        N/A        N/A       

MENONBE.NS      Menon Bearings Limited                                       52.80      +8.80      +20.00%    2.334M     65,713     2.952B     25.05     

RPOWER.NS       Reliance Power Limited                                       3.3000     +0.5500    +20.00%    127.814M   18.439M    9.223B     N/A       

11DPD.BO        Nippon India Mutual Fund                                     0.0600     +0.0100    +20.00%    190        N/A        N/A        N/A       

ABFRLPP-E1.NS   Aditya Birla Rs.5 ppd up                                     105.65     +17.60     +19.99%    1.238M     N/A        N/A        N/A       

500110.BO       Chennai Petroleum Corporation Limited                        64.55      -0.15      -0.23%     42,765     61,584     9.612B     N/A       

ABFRLPP.BO      Aditya Birla Fashion and Retai                               106.05     +17.65     +19.97%    387,703    N/A        N/A        N/A       

RADIOCITY.NS    Music Broadcast Limited                                      21.35      +3.55      +19.94%    12.657M    1.013M     7.38B      124.13    

RADIOCITY.BO    Music Broadcast Limited                                      21.35      +3.55      +19.94%    898,070    90,236     7.38B      124.13    

MENONBE.BO      Menon Bearings Limited                                       52.65      +8.75      +19.93%    137,065    8,648      2.951B     24.98     

MTNL.BO         Mahanagar Telephone Nigam Limited                            10.72      +1.78      +19.91%    1.142M     156,275    6.754B     N/A       


...and so on.


查看完整回答
反對 回復 2023-12-26
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號