通过 Xpath 查找元素。如何在 Xpath 中拆分我不想要的元素

Find element by Xpath. How to split the element I don't want inside the Xpath

我尝试使用 Selenium 抓取网站。 当我尝试获取硬币名称时遇到问题。因为 'td' 里面有 2 个元素 我怎样才能摆脱我不想要的另一个元素。或者只跟踪它的第一个元素。 (我发现了这个 但我不确定它是否能解决我的问题)

这是我的全部代码

#driver chrome def
website = 'https://www.bitkub.com/fee/cryptocurrency'
path = r"C:\Users\USER\Downloads\chromedriver.exe"
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get(website)

#giving variable
coin_name = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tbody//tr//td[2]//span")))]
chain_name = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tbody//tr//td[3]//div")))]
withdrawal_fees = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tbody//tr//td[4]//div")))]
#print(coin_name)
#print(chain_name)
#print(withdrawal_fees)


#for loop make list

for coin, chains, wdf in zip(coin_name, chain_name, withdrawal_fees):
    print("Coin name: {} Chain: {} Fee: {}".format(coin, chains, wdf))

coin_name 的输入(我提到它有 2 个元素)

['Civic(CVC)', '(CVC)', 'Bitcoin SV(BSV)', '(BSV)', 'Ethereum(ETH)', '(ETH)', 'Bitkub Coin(KUB)', '(KUB)', 'Compound(COMP)', '(COMP)', 'Curve DAO Token(CRV)', '(CRV)', .... ]

网站上的元素是这样的

我希望输入看起来像这样,这样我就可以用它制作数据框

['Civic(CVC)', 'Bitcoin SV(BSV)', 'Ethereum(ETH)', 'Bitkub Coin(KUB)', 'Compound(COMP)', 'Curve DAO Token(CRV)', .... ]

希望对您有所帮助:

for name in coin_name:
    if name[0] == "(":
        coin_name.remove(name)

根据您当前的输出:

['Civic(CVC)', '(CVC)', 'Bitcoin SV(BSV)', '(BSV)', 'Ethereum(ETH)', '(ETH)', 'Bitkub Coin(KUB)', '(KUB)', 'Compound(COMP)', '(COMP)', 'Curve DAO Token(CRV)', '(CRV)', .... ]

您可以跳过每个替代元素并使用 创建一个新列表,如下所示:

# coin_name = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tbody//tr//td[2]//span")))]
coin_name = ['Civic(CVC)', '(CVC)', 'Bitcoin SV(BSV)', '(BSV)', 'Ethereum(ETH)', '(ETH)', 'Bitkub Coin(KUB)', '(KUB)', 'Compound(COMP)', '(COMP)', 'Curve DAO Token(CRV)', '(CRV)']
res = [coin_name[i] for i in range(len(coin_name)) if i % 2 == 0]
print (res)

控制台输出:

['Civic(CVC)', 'Bitcoin SV(BSV)', 'Ethereum(ETH)', 'Bitkub Coin(KUB)', 'Compound(COMP)', 'Curve DAO Token(CRV)']