Dfhaa_DK
Dfhaa_DK

Reputation: 131

Need help scraping a web-page

I started a mini project where I want to retrieve coin name, price, coin market-cap, circulating supply and volume for the first 100 coins on the first page. Until now, (after asking several questions and getting good inputs) I am able to retrieve all the coin names on the first page. However, with prices I only get the first 11 names, and coin-market cap I get nothing. I really want to be collect the data so I can start doing some data analysis, but I always hit a wall. Can someone point me further in the right direction?? I am aware there is an api, but I want to expand my knowledge.

import requests
from lxml import html
from selenium import webdriver

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

driver = webdriver.Chrome()

driver.get(url)

coin_name = [name.text for name in driver.find_elements_by_xpath('//td[3]/a[@class="cmc-link" and starts-with(@href, "/currencies/")]')]
print(coin_name)
print(len(coin_name))
price = [p.text for p in driver.find_elements_by_xpath('//td[4]/div/a[@class = "cmc-link" and contains(@href, "/markets/")]')]
print(price)
print(len(price))

market_cap = [m.text for m in driver.find_elements_by_xpath('//p[@class = "cmc-link" and contains(@style, "white-space:nowrap")]//p[@color="text"]')]

print(len(market_cap))

Upvotes: 1

Views: 293

Answers (1)

Abhishek Rai
Abhishek Rai

Reputation: 2237

You need to scroll the elements into the view. Just keep sending a page down.

import time

import requests
from lxml import html
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

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

driver = webdriver.Chrome(executable_path='C:/chromedriver.exe')

driver.get(url)
driver.maximize_window()
for i in range(22):
    coin_name = driver.find_elements_by_xpath('//td[3]/a[@class="cmc-link" and starts-with(@href, "/currencies/")]')

    prices = driver.find_elements_by_xpath('//td[4]/div/a[@class = "cmc-link" and contains(@href, "/markets/")]')

    market_caps = driver.find_elements_by_xpath('//p[@class="sc-1eb5slv-0 kDEzev"]')
    
    html = driver.find_element_by_tag_name('html')
    html.send_keys(Keys.PAGE_DOWN)
    for coin, price, cap in zip(coin_name, prices, market_caps):
        print("Coin :", coin.text, "Price :", price.text, "Market_caps:", cap.text)

Output:-

CETH Price : $648.10 Market cap : $435,982,385,626
Coin : XRP
XRP Price : $0.573805 Market cap : $73,825,115,056
Coin : Tether
USDT Price : $1.00 Market cap : $26,053,047,457
Coin : Litecoin
LTC Price : $117.47 Market cap : $20,302,149,320
Coin : Bitcoin Cash
BCH Price : $350.85 Market cap : $7,768,463,654
Coin : Chainlink
LINK Price : $13.34 Market cap : $6,526,170,500
Coin : Cardano
ADA Price : $0.163470 Market cap : $5,300,831,845
Coin : Binance Coin
BNB Price : $34.82 Market cap : $5,085,969,556
Coin : Polkadot
DOT Price : $5.25 Market cap : $5,027,979,819
Coin : Stellar
XLM Price : $0.178429 Market cap : $4,683,566,948

Upvotes: 1

Related Questions