首页 > 解决方案 > 试图刮掉 coinmarketcap 并获得“无”

问题描述

我正在使用 python 构建一个基本脚本来从coinmarketcap中抓取一些数据,但我没有得到任何数据,我不知道为什么,请你帮帮我?

from bs4 import BeautifulSoup as S
import requests

c = input('enter your coin')
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(id="priceValue___11gHJ")
print(x)```

标签: pythonweb-scraping

解决方案


我已经测试了您的代码并进行了一些更改以使其正常工作。

您要获取的字段使用类而不是 id。

看看它。

在此处输入图像描述

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

c = input('enter your coin')
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_='priceValue___11gHJ').text
print(x)

推荐阅读