2017-07-26 6 views
0

株価を確認するスクリプトがあります。 Yahooは何かを変え、今や株価ではなく%変化を得る。以下は元のスクリプトです。私がそれを走らせるとき、私は+ 0.77(+ 0.03%)を得ました、むしろ2,477.83。私は本当に見唯一の違いは、次のとおりです。beautifulsoupでリクエストしてyahooファイナンスの株価をご希望の場合

データreactid = "36"

データreactid = "35"。

35に変更すると失敗します。 36作品が表示されますが%変化のみを示します。私は株価を望んでいますが、変化率はありません。

ご協力いただきありがとうございます。

import urllib.request 
from bs4 import BeautifulSoup 


# S&P 500 
page = urllib.request.urlopen("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC") 
content = page.read().decode('utf-8') 
soup = BeautifulSoup(content, 'html.parser') 
valsp = soup.find("span", {"data-reactid": "36"}).decode_contents(formatter="html") 
print(valsp) 

答えて

1

属性data-reactid = "35"を持つ複数のspan要素がありますので、class属性で必要なものを選択してください。

import urllib.request 
from bs4 import BeautifulSoup 

# S&P 500 
page = urllib.request.urlopen("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC") 
content = page.read().decode('utf-8') 
soup = BeautifulSoup(content, 'html.parser') 
# print (soup) 
valsp = soup.find("span", {"data-reactid": "35", "class" : "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"}).decode_contents(formatter="html") 
print(valsp) 

出力:

2,477.83 

唯一の変更は、あなたのコード内でこの行です:

valsp = soup.find("span", {"data-reactid":"35"}).decode_contents(formatter="html") 

valsp = soup.find("span", {"data-reactid": "35", "class" : "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"}).decode_contents(formatter="html") 
+0

にありがとうございました! –

関連する問題