Issue
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.
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.
Solution
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
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.