【问题标题】:Want to scrape coinmarketcap but am only getting first 10 currency of 100想刮coinmarketcap,但我只得到100的前10个货币
【发布时间】:2021-07-02 23:47:58
【问题描述】:

我想通过这段代码刮掉,但我只得到页面中的前 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) ```

【问题讨论】:

  • 我建议你使用CoinMarketCap API来获取数据。前 10K 通话是免费的。您抓取数据的方式不稳定,因为每次部署新更改时都会自动生成类名 sc-1teo54s-2 fZIJcI
  • 但是python没有显示所有页面包含有什么问题?
  • 您需要先向下滚动到页脚,以便加载所有数据,然后您可以抓取所需的数据。请在下面查看我的答案,如果我帮助了您,请接受:)

标签: python web-scraping beautifulsoup


【解决方案1】:

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

BeautifulSoup 不允许您向下滚动页面,因此您需要使用像 Selenium 这样的 WebDriver。

【讨论】:

    【解决方案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
    

    【讨论】:

    • 你试过@data data这个脚本了吗?有什么反馈?
    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 2022-01-15
    • 1970-01-01
    • 2021-04-08
    • 2021-03-24
    • 1970-01-01
    • 2021-03-31
    • 2023-02-23
    相关资源
    最近更新 更多