简体   繁体   English

为什么我的变量返回 None 而不是股票价格?

[英]Why does my variable return None instead of the stock price?

I tried to scrape the stock price from https://finance.yahoo.com/quote/TSLA/ using BeautifulSoup, but for some reason my program doesn't print the stock price and instead returns "None".我试图使用 BeautifulSoup 从https://finance.yahoo.com/quote/TSLA/抓取股票价格,但由于某种原因我的程序没有打印股票价格而是返回“无”。 How do I correctly scrape the stock price?如何正确抓取股价? My code:我的代码:

from bs4 import BeautifulSoup
import requests
import time
headers = {"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"}
url = "https://finance.yahoo.com/quote/TSLA/"
time.sleep(10)
GetUrl = requests.get(url)
soup = BeautifulSoup(GetUrl.text)
StockPrice = soup.find({"Fw(b) Fz(36px) Mb(-4px) D(ib)"})
print(StockPrice)

Here is the place that I found the data, which I copied to soup.find().这是我找到数据的地方,我将其复制到 soup.find()。

In soup.find() , use the class_= parameter:soup.find()中,使用class_=参数:

import requests
from bs4 import BeautifulSoup

url = "https://finance.yahoo.com/quote/TSLA/"

GetUrl = requests.get(url)
soup = BeautifulSoup(GetUrl.text, "html.parser")

# use class_="..."
StockPrice = soup.find(class_="Fw(b) Fz(36px) Mb(-4px) D(ib)")

print(StockPrice.text)

Prints:印刷:

975.93

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM