import requests
from money import Money
from bs4 import BeautifulSoup
response = requests.get("https://finance.yahoo.com/quote/VTI?p=VTI")
soup = BeautifulSoup(response.content, "lxml")
price = soup.find('span', attrs = {"data-reactid": "34"})
这将带回“无”的值。有什么我想念的吗?使用其他页面,以下工作正常:
response = requests.get("https://finance.yahoo.com/lookup?s=VTI")
soup = BeautifulSoup(response.content,"lxml")
price = soup.find('td', attrs={"data-reactid": "59"})
不幸的是,该搜索页面并不总是与第一个结果完全匹配(相反,搜索VXUS会将vxus带回第二个结果),因此我希望找到可以始终如一地工作的东西,而且我认为从实际页面中提取信息会工作最好。
拉升141.28值的最佳方法是什么?
最佳答案
import requests
from bs4 import BeautifulSoup
import json
response = requests.get("https://finance.yahoo.com/quote/VTI?p=VTI")
soup = BeautifulSoup(response.content)
price = soup.findAll('script')
regularMarketPrice
a = price[-3].contents[0]
jjj = json.loads(a[111:-12])
jjj['context']['dispatcher']['stores']['StreamDataStore']['quoteData']['VTI']['regularMarketPrice']
这可能会帮助您,首先获取scriptdata,然后将其转换为json,您可以找到所需的数据
关于python - 用beautifulsoup拉当前股价(Yahoo),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55056325/