0

我正在尝试从bscscan抓取数据。我的代码如下所示。

我不断收到超时异常。但是删除此行后:

driver.find_element_by_xpath('/html/body/div[4]/table/thead/tr/th[2]/a').click()

问题不再发生。有谁知道如何解决这个问题?AGE请注意,在将数据导出到 csv 之前,我需要单击按钮。

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

PATH = 'C:/Users/XX/Downloads/chromedriver_win32/chromedriver.exe'
driver = webdriver.Chrome(PATH)

driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://bscscan.com/token/0x20de22029ab63cf9a7cf5feb2b737ca1ee4c82a6#tokenTrade')
print(driver.title)
wait = WebDriverWait(driver, 20)
try:
  wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='btnCookie']"))).click()
except:
  pass
Txn_Hash = []
Age = []
Maker = []
Taker = []
Price = []
DEX = []
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "dextrackeriframe")))
#wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Age"))).click()
driver.find_element_by_xpath('/html/body/div[4]/table/thead/tr/th[2]/a').click()
table_size = len(driver.find_elements(By.XPATH, "//thead[@class='thead-light']/following-sibling::tbody//tr"))
print(table_size)
j = 1
for i in range(table_size):
    tnx_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/descendant::a[1]"))).text
    age_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[3]"))).text
    maker_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[4]"))).text
    Taker_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[6]"))).text
    Price_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[7]"))).text
    DEX_href = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[8]/img"))).get_attribute('src')
    Txn_Hash.append(tnx_text)
    Age.append(age_text)
    Maker.append(maker_text)
    Taker.append(Taker_text)
    Price.append(Price_text)
    DEX.append(DEX_href)
    j = j + 1

print(Txn_Hash)
print(Age)
print(Maker)
print(Taker)
print(Price)
print(DEX)

data = {
     'Transaction_HashKey': Txn_Hash,
     'Age': Age,
     'Maker': Maker,
     'Taker' : Taker,
     'Price' :Price,
     'DEX' : DEX
    }
df = pd.DataFrame.from_dict(data)
df.to_csv('output.csv', index = 0)
4

1 回答 1

0

您收到此错误是因为以下代码行没有返回任何内容。

wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[3]")))

这行代码是有效的,如果你不点击AGE按钮,但是执行了点击动作之后,什么都没有返回。另外,您缺少span标签。

将上面的代码行替换为以下内容:

age_text = wait.until(EC.visibility_of_element_located((By.XPATH, f"//th[contains(text(),'Txn Hash')]/ancestor::thead/following-sibling::tbody/tr[{j}]/td[2]/span"))).text
于 2021-09-17T06:09:56.670 回答