试图在CNBC上抓取符号的价格变化,但我得到一个空的list
,我做错了什么?
from bs4 import BeautifulSoup
import requests
url = "https://www.cnbc.com/nasdaq-100/"
response = requests.get(url)
content = response.text
soup = BeautifulSoup(content, "html.parser")
data = soup.find_all("td", {"class": "BasicTable-quoteDecline"})
print(data)
如上所述,内容是动态生成的,由响应JSON数据的其他调用提供。
如果你知道这些符号,你可以请求它们并获得所有信息(AMD和TSLA):
https://quote.cnbc.com/quote-html-webservice/restQuote/symbolType/symbol?symbols=AMD%7CTSLA&requestMethod=itv&noform=1&partnerId=2&fund=1&exthrs=1&output=json&events=1
这将给你一个所请求的符号和附加数据的列表:
[{'symbol': 'AMD', 'symbolType': 'symbol', 'code': 0, 'name': 'Advanced Micro Devices Inc', 'shortName': 'AMD', 'onAirName': 'Advanced Micro', 'altName': 'Advanced Micro Devices', 'last': '76.95', 'last_timedate': '4:15 PM EDT', 'last_time': '2022-07-11T16:15:00.000-0400', 'changetype': 'DOWN', 'type': 'STOCK', 'subType': 'Common Stock', 'exchange': 'NASDAQ', 'source': 'Last NASDAQ LS, VOL From CTA', 'open': '77.85', 'high': '78.29', 'low': '75.84', 'change': '-2.40', 'change_pct': '-3.02%', 'currencyCode': 'USD', 'volume': '72,260,709', 'volume_alt': '72.3M', 'provider': 'CNBC QUOTE CACHE', 'previous_day_closing': '79.35', 'altSymbol': 'AMD.O', 'realTime': 'true', 'curmktstatus': 'POST_MKT', 'pe': '28.71', 'mktcapView': '124.698B', 'beta': '1.93', 'tendayavgvol': '90.44M', 'pcttendayvol': '0.8399', 'yrhiprice': '164.46', 'yrhidate': '11/30/21', 'yrloprice': '71.60', 'yrlodate': '07/05/22', 'eps': '2.68', 'sharesout': '1.621B', 'revenuettm': '18.876B', 'ROETTM': '10.98%', 'NETPROFTTM': '17.94%', 'GROSMGNTTM': '48.53%', 'TTMEBITD': '4.767B', 'DEBTEQTYQ': '3.23%', 'fpe': '17.73', 'feps': '4.34', 'streamable': '1', 'issue_id': '44307', 'issuer_id': '9238', 'countryCode': 'US', 'timeZone': 'EDT', 'ExtendedMktQuote': {'type': 'POST_MKT', 'source': 'Last NASDAQ LS, VOL From CTA', 'last': '77.18', 'last_timedate': '4:53 PM EDT', 'change': '+0.23', 'change_pct': '+0.30%', 'volume': '2,970,833', 'volume_alt': '3.0M', 'changetype': 'UP'}, 'EventData': {'next_earnings_date': '07/25/2022(est)', 'next_earnings_date_today': 'N', 'announce_time': 'A', 'yrhiind': 'N', 'yrloind': 'N', 'is_halted': 'N'}}, {'symbol': 'TSLA', 'symbolType': 'symbol', 'code': 0, 'name': 'Tesla Inc', 'shortName': 'TSLA', 'onAirName': 'Tesla', 'altName': 'Tesla', 'last': '703.03', 'last_timedate': '4:00 PM EDT', 'last_time': '2022-07-11T16:00:00.000-0400', 'changetype': 'DOWN', 'type': 'STOCK', 'subType': 'Common Stock', 'exchange': 'NASDAQ', 'source': 'Last NASDAQ LS, VOL From CTA', 'open': '756.31', 'high': '759.19', 'low': '700.88', 'change': '-49.26', 'change_pct': '-6.55%', 'currencyCode': 'USD', 'volume': '31,878,440', 'volume_alt': '31.9M', 'provider': 'CNBC QUOTE CACHE', 'previous_day_closing': '752.29', 'altSymbol': 'TSLA.O', 'realTime': 'true', 'curmktstatus': 'POST_MKT', 'pe': '95.02', 'mktcapView': '706.025B', 'beta': '2.13', 'tendayavgvol': '28.93M', 'pcttendayvol': '1.1417', 'yrhiprice': '1,243.49', 'yrhidate': '11/04/21', 'yrloprice': '620.57', 'yrlodate': '05/24/22', 'eps': '7.40', 'sharesout': '1.004B', 'revenuettm': '62.19B', 'ROETTM': '29.42%', 'NETPROFTTM': '13.60%', 'GROSMGNTTM': '27.10%', 'TTMEBITD': '12.776B', 'DEBTEQTYQ': '14.12%', 'fpe': '57.21', 'feps': '12.29', 'streamable': '1', 'issue_id': '24812378', 'issuer_id': '74213', 'countryCode': 'US', 'timeZone': 'EDT', 'ExtendedMktQuote': {'type': 'POST_MKT', 'source': 'Last NASDAQ LS, VOL From CTA', 'last': '702.65', 'last_timedate': '4:53 PM EDT', 'change': '-0.38', 'change_pct': '-0.05%', 'volume': '206,060', 'volume_alt': '206.1K', 'changetype': 'DOWN'}, 'EventData': {'next_earnings_date': '07/20/2022', 'next_earnings_date_today': 'N', 'announce_time': 'A', 'yrhiind': 'N', 'yrloind': 'N', 'is_halted': 'N'}}]
import requests
jsonData =requests.get('https://quote.cnbc.com/quote-html-webservice/restQuote/symbolType/symbol?symbols=AMD%7CTSLA&requestMethod=itv&noform=1&partnerId=2&fund=1&exthrs=1&output=json&events=1').json()
for s in jsonData['FormattedQuoteResult']['FormattedQuote']:
print(s['symbol'],s['change'] )
输出AMD -2.40
TSLA -49.26
import requests
jsonData =requests.get('https://quote.cnbc.com/quote-html-webservice/restQuote/symbolType/symbol?symbols=AMD%7CTSLA&requestMethod=itv&noform=1&partnerId=2&fund=1&exthrs=1&output=json&events=1').json()
for s in jsonData['FormattedQuoteResult']['FormattedQuote']:
print(s['symbol'],s['change'] )
AMD -2.40
TSLA -49.26
否则你可以使用selenium
来渲染网站,就像浏览器一样。