【发布时间】:2021-12-25 20:32:45
【问题描述】:
我正在创建一个删除货币名称和价格的脚本。每当我运行我的脚本时,它都可以正常工作,但问题是它不会以订单格式打印,就像比特币价格是$65,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()
提前致谢。
【问题讨论】:
-
请发布最小可重现代码:minimal reproducible example。打印某种格式的东西可以用 f-strings 来完成,但我们不知道你的代码有什么问题或者什么可以正确完成。
-
你为什么使用硒?它不适用于网络抓取。改用 python3 请求:docs.python-requests.org/en/latest
标签: python selenium xpath list-comprehension webdriverwait