如何使用 Selenium 和 Python 以有序格式编写名称和价格

How to write names and price in ordered format using Selenium and Python

我正在创建一个脚本来删除货币名称和价格。每当我 运行 我的脚本它工作得很好,但问题是它不会以订单格式打印,就像比特币价格是 ,056.71 它会在比特币行中写另一个硬币价格。

通过这种方式,它向每一行写入随机值

这是我的代码:

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

提前致谢。

使用下面的代码

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

要抓取 货币名称价格,您可以使用 List Comprehension to collect the currency names and prices inducing induce for the visibility_of_all_elements_located() and you can use either of the following :

  • 代码块:

    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()
    
  • 控制台输出:

    Name:Bitcoin current price is:,446.62
    Name:Ethereum current price is:,611.20
    Name:Binance Coin current price is:6.55
    Name:Tether current price is:[=11=].9999
    Name:Solana current price is:5.89
    Name:Cardano current price is:.05
    Name:XRP current price is:.19
    Name:Polkadot current price is:.57
    Name:Dogecoin current price is:[=11=].263
    Name:USD Coin current price is:[=11=].9996
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC