我正在學習如何在 Python 中使用 Selenium,并且一直在玩一些不同的東西。我一直遇到無法找到任何課程的問題。我可以通過 xpath 定位和列印資料,但我找不到類。
此腳本的目標是從網站上的表格中收集一個數字和當前時間,然后將這些專案附加到 CSV 檔案中。
網站:https ://bitinfocharts.com/top-100-richest-dogecoin-addresses.html
任何建議或指導將不勝感激,因為我是 python 新手。謝謝你。
代碼:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
import pandas as pd
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import csv
from datetime import datetime
from selenium.webdriver.common.by import By
#Open ChromeDriver
PATH = ('/Users/brandon/Desktop/chromedriver')
driver = webdriver.Chrome(PATH)
driver.get("https://bitinfocharts.com/top-100-richest-dogecoin-addresses.html")
driver.implicitly_wait(10)
driver.maximize_window()
#Creates the Time
now = datetime.now()
current_time = now.strftime("%m/%d/%Y, %H:%M:%S")
#####
#Identify the section of page
page = driver.find_element(By.CLASS_NAME,'table table-condensed bb')
time.sleep(3)
#Gather the data
for page in pages():
num_of_wallets = page.find_element(By.XPATH,
"//html/body/div[5]/table[1]/tbody/tr[10]/td[2]").text
table_dict = {"Time":current_time,
"Wallets":num_of_wallets}
file = open('dogedata.csv', 'a')
try:
file.write(f'{current_time},{num_of_wallets}')
finally:
file.close()
uj5u.com熱心網友回復:
table table-condensed bb實際上包含3個類名。
因此,基于多個類名定位元素的最佳方法是使用 css 選擇器或 xpath,例如:
page = driver.find_element(By.CSS_SELECTOR,'.table.table-condensed.bb')
或者
page = driver.find_element(By.XPATH,"//*[@class='table table-condensed bb']")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414614.html
標籤:
上一篇:我不能將歌劇與硒一起使用
