1

我正在尝试获取 coinmarketcap 加密货币的价格。不幸的是,它不起作用。任何人都可以帮忙吗?

我想显示这枚硬币的价格:https ://coinmarketcap.com/currencies/bombcrypto/

我的代码:

#!/usr/bin/env python3
from bs4 import BeautifulSoup as S
import requests

c = input('bombcrypto')
url = f'https://coinmarketcap.com/currencies/{c}/'

headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}

r = requests.get(url,headers=headers)
soup = S(r.content,'html.parser')
print(f'the price of {c} now is ')
x = soup.find(class_='sc-16r8icm-0 kjciSH priceTitle').text
print(x)

谢谢你

4

1 回答 1

1

您的代码有几个问题。确保在查找元素路径时正确遵循网页的结构。我还在第 5 行更改了输入语句。 元素

#!/usr/bin/env python3
from bs4 import BeautifulSoup as S
import requests

c = input("Crypto: ")#'bombcrypto'
url = f'https://coinmarketcap.com/currencies/{c}/'

headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}

r = requests.get(url,headers=headers)
soup = S(r.content,'html.parser')
print(f'the price of {c} now is ')
x = soup.find(class_='sc-16r8icm-0 kjciSH priceTitle').findChild(class_="priceValue").findChild('span')
print(x.text)
于 2021-12-09T02:41:35.787 回答