我正在尝试从雅虎财务页面中抓取每日更改值,例如BTC:https://finance.yahoo.com/quote/BTC-USD和屏幕截图 of value。
下面是我的代码:
import requests, json
from bs4 import BeautifulSoup
btc_url = 'https://finance.yahoo.com/quote/BTC-USD'
btc_page = requests.get(btc_url)
btc_soup = BeautifulSoup(btc_page.content, 'html.parser')
btc_change = btc_soup.find('div' , class_='D(ib) Mend(20px)').find('fin-streamer' ,class_ ='Fw(500) Pstart(8px) Fz(24px)').text
print (btc_change)我确实得到了返回的数据,但是无论网页上的实际内容是什么,它的值总是-661.20。有人能检查一下,如果我做错了什么,可以告诉我吗?谢谢
发布于 2022-07-20 23:21:24
您得到的是无意义的数据,因为请求看起来不合法。您需要包括一个用户代理。
我正试图从雅虎金融公司( Yahoo )中摘取股票价格,而我的目标是价格。但是,当我运行我的代码时,输出中会出现“无”。
试试这个:
import requests, json
from bs4 import BeautifulSoup
btc_url = 'https://finance.yahoo.com/quote/BTC-USD'
headers = {'User-agent': 'Mozilla/5.0'}
btc_page = requests.get(btc_url, headers=headers)
btc_soup = BeautifulSoup(btc_page.content, 'html.parser')
btc_change = btc_soup.find('fin-streamer', {'data-symbol':'BTC-USD', 'data-field':'regularMarketChange'}).text
print(btc_change)https://stackoverflow.com/questions/73058531
复制相似问题