想要刮硬币市场的上限,但我只得到前10个100货币

2022-12-01 05:36:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过这个代码刮,但我只得到了页面中的前10个货币,但页面包含100个货币,其他90个在哪里

from bs4 import BeautifulSoup as S
import requests

url = 'https://coinmarketcap.com/'

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')

price = soup.find_all('td')

for coin in soup.find_all(class_="sc-1teo54s-2 fZIJcI"):
    print(coin) ```


Tags: 代码fromimporturlas货币页面allfindrequestsheaderscoinsoupbs4beautifulsoup
2条回答

尝试以下操作以获得100个结果

import requests

link = 'https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing'

params = {
    'start': 1,
    'limit': 100,
    'sortBy': 'market_cap',
    'sortType': 'desc',
    'convert': 'USD,BTC,ETH',
    'cryptoType': 'all',
    'tagType': 'all',
    'audited': 'false',
    'aux': 'ath,atl,high24h,low24h,num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply,volume_7d,volume_30d'
}

with requests.Session() as s:
    s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
    res = s.get(link,params=params)
    for item in res.json()['data']['cryptoCurrencyList']:
        rank = item['cmcRank']
        coin_name = item['name']
        print(rank,coin_name)

输出如下(截断):

1 Bitcoin
2 Ethereum
3 Tether
4 Binance Coin
5 Cardano
6 Dogecoin
7 XRP
8 USD Coin
9 Polkadot
10 Binance USD
11 Uniswap
12 Solana
13 Bitcoin Cash
14 Litecoin
15 Chainlink
16 Polygon
17 Wrapped Bitcoin
18 Ethereum Classic
19 THETA
20 Internet Computer

向下滚动时,CoinMarketCap网站将加载数据。这意味着,如果你想抓取所有显示的加密货币数据,你需要先向下滚动到页面底部

BeautifulSoup不允许您向下滚动页面,因此您需要使用类似Selenium的Web驱动程序