-3

I'm creating a script that scraps currency names and prices. whenever I run my script it works perfectly fine but the problem is it does not print in order format like if the bitcoin price is $65,056.71 it will write another coin price in bitcoin line.

enter image description here

In this way, it writes random values to each line

Here is my code:

from selenium import webdriver

driver = webdriver.Chrome(executable_path="chromedriver.exe")
driver.get("https://coinmarketcap.com/")
driver.implicitly_wait(10)

coins = set()
coin_names = driver.find_elements_by_class_name('iworPT')
print(coin_names)

for names in coin_names:
    coins.add(names.text)

    for coins_val in coins:
        print(coins_val)


# coins price
coinsprice = []
get_coin_price = driver.find_elements_by_class_name('cLgOOr')

for price in get_coin_price:
    coinsprice.append(price.text)

    for price_val in coinsprice:
        print(price_val)


with open('coins.txt', 'w') as f:
    for coins_name, prices in zip(coins,coinsprice):
        f.write(coins_name + ": " + prices + "\n")

driver.close()

Thanks in advance.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Kirito-Kun
  • 93
  • 8
  • 3
    Please post a minimal reproducible code: [MCVE]. Printing things in a format can be done with f-strings, but we don't know what is wrong in your code or what can be done properly. – j1-lee Nov 14 '21 at 06:30
  • 1
    why are you using selenium ? it is not meant for web scraping . use python3 requests instead : https://docs.python-requests.org/en/latest/ – Shekhar Samanta Nov 14 '21 at 06:51

2 Answers2

0

To scrape the currency names and prices you can use List Comprehension to collect the currency names and prices inducing induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Code Block:

    driver.get("https://coinmarketcap.com/")
    cryptos = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='cmc-link' and contains(@href, 'currencies')]//p[@color='text' and @font-weight='semibold']")))]
    prices = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='cmc-link' and contains(@href, 'currencies')]//p[@color='text' and @font-weight='semibold']//following::td[1]//a")))]
    for i,j in zip(cryptos, prices):
        print(f"Name:{i} current price is:{j}")
    driver.quit()
    
  • Console Output:

    Name:Bitcoin current price is:$64,446.62
    Name:Ethereum current price is:$4,611.20
    Name:Binance Coin current price is:$646.55
    Name:Tether current price is:$0.9999
    Name:Solana current price is:$235.89
    Name:Cardano current price is:$2.05
    Name:XRP current price is:$1.19
    Name:Polkadot current price is:$46.57
    Name:Dogecoin current price is:$0.263
    Name:USD Coin current price is:$0.9996
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

Use the code Below

import requests
import json
import time
from random import randint
import csv

def parser():
    r = requests.get('https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing?start=1&limit=10000&sortBy=market_cap&sortType=desc&convert=USD&cryptoType=all&tagType=all',
                     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'})
    json_data = json.loads(r.text)
    i = 2
    for x in json_data['data']['cryptoCurrencyList']:
        try:
            rank = x['cmcRank']
        except:
            rank = None
        try:
            Name = x['name']
        except:
            Name = None
        try:
            Symbol = x['symbol']
        except:
            Symbol = None
        try:
            Market_Cap = x['quotes'][0]['marketCap']
        except:
            Market_Cap = None
        try:
            Price = x['quotes'][0]['price']
        except:
            Price = None
        try:
            Circulating_Supply = x['circulatingSupply']
        except:
            Circulating_Supply = None
        try:
            Volume_24h = x['quotes'][0]['volume24h']
        except:
            Volume_24h = None
        try:
            percentChange1h = str(round(x['quotes'][0]['percentChange1h'],2))+'%'
        except:
            percentChange1h = None
        try:
            percentChange24h = str(round(x['quotes'][0]['percentChange24h'],2))+'%'
        except:
            percentChange24h = None
        try:
            percentChange7d = str(round(x['quotes'][0]['percentChange7d'],2))+'%'
        except:
            percentChange7d = None
        writer.writerow([rank, Name, Symbol, Market_Cap, Price, Circulating_Supply, Volume_24h, percentChange1h, percentChange24h, percentChange7d])

with open('Out.csv','w',newline='',encoding='utf-8')as export:
    writer = csv.writer(export)
    writer.writerow(['Rank', 'Name', 'Symbol', 'Market_Cap', 'Price', 'Circulating_Supply', 'Volume_24h', 'percentChange1h', 'percentChange24h', 'percentChange7d'])
    parser()
Shekhar Samanta
  • 875
  • 2
  • 12
  • 25