我正试图得到一种市值最高的密码货币的价格。很不幸它不起作用了。有人能帮忙吗?
我想展示一下这枚硬币的价格: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)谢谢
发布于 2021-12-09 02:41:35
您的代码有几个问题。在通过元素找到路径时,请确保正确地遵循网页的结构。我还更改了第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)https://stackoverflow.com/questions/70283768
复制相似问题