获取 "None" 用于 BS4 网络抓取

Getting "None" for BS4 web scraping

所以我正在尝试创建一个可以获取比特币价格的代码 由于某些原因 运行 此代码将导致 None 的输出,但是我想要当前比特币价格的输出,我该如何解决?

url = 'https://www.google.com/search?q=bitcoin+price'
        r = requests.get(url)
        soup = BeautifulSoup(r.text, 'html.parser')

        text = soup.find('span', {'class':'vpclqee'})
        print(text)

也许你有一个不必要的v尝试写:

text = soup.find('span', {'class':'pclqee'})

而不是:

text = soup.find('span', {'class':'vpclqee'})

如果您对使用 Google 的比特币价格没有限制,其他一些网站可以更轻松地访问此值,例如 CoinMarketCap:

from bs4 import BeautifulSoup
import requests

url = 'https://coinmarketcap.com/currencies/bitcoin/'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')

text = soup.find_all('div', {'class':"priceValue"})

for elem in text:
    print(elem.get_text())

但请注意,这不适合任何 real-time 更新,因为我认为它更新得太慢了。

输出:

,878.01