为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Selinium Python 获取 td web-table 中的文本

如何使用 Selinium Python 获取 td web-table 中的文本

慕的地6264312 2023-10-24 20:26:01
我正在尝试从 td webtable 中提取文本,但无法找到该元素;该表没有类或 ID,因此我尝试使用 xpath 但没有成功。任何帮助是极大的赞赏。driver = webdriver.Chrome(ChromeDriverManager().install())driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")pair_1 = driver.find_element(By.XPATH("html/body/div[@id ='wrap']/div[@id='content-wrap']/div[@id='content']/div[contains(@class, ' ')]/div[contains(@class, ' ')]/div[contains(@class, 'ui-helper-resert')]/div[@id ='alignInfo']/div[@id ='alignments']/table/tbody/tr[2]/td[1]"))print(pair_1.text)#ORpair_1.get_attribute("innerHTML")print(pair_1)返回以下错误TypeError: 'str' object is not callable更简单的 xpathpair_1 = driver.find_element_by_xpath("//table/tbody/tr[2]/td[1]")print(pair_1.text)返回这个Looking for [chromedriver 80.0.3987.06 mac6] driver in cache File found in cache by path [/Users/usr/.wdm/drivers/chromedriver/80.0.3987.06/mac6/chromedriver]网站和 HTML
查看完整描述

3 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

尝试以下操作


import time


from selenium import webdriver


driver = webdriver.Chrome()

driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")

time.sleep(2)


pair_1 = driver.find_element_by_css_selector(

    "#alignments > div:nth-child(1) > table > tbody > tr:nth-child(2) > td:nth-child(2)"

).get_attribute("innerHTML")



print(pair_1)

在 Windows 上,chrome 版本 80


选择文本>右键单击>检查>右键单击>检查>右键单击<td>复制选择器

https://img1.sycdn.imooc.com/6537b8320001850105950223.jpg

https://img1.sycdn.imooc.com/6537b83b00010de305970440.jpg

查看完整回答
反对 回复 2023-10-24
?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

从Induce和()中得到“正向引物”、“反向引物”和“产物长度”的值。Primer pair 1WebDriverWait()visibility_of_element_locatedXPath


1.创建一个项目列表,然后进行迭代。


from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


product_list=['Forward primer','Reverse primer','Product length']

driver=webdriver.Chrome()

driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")

for item in product_list:

    print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//h2[text()='Primer pair 1']/following::table[1]//th[text()='{}']/following-sibling::td[1]".format(item)))).text)

输出:


GAGGGCATACCCCTCGTAGA

CTGCCGTTTTCCGTAGGACT

506

要从所有引物对获取数据,请尝试此操作。


1.创建引物列表,然后迭代。


2.创建项目列表,然后进行迭代。


from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


driver=webdriver.Chrome()

driver.get("https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check")


list_primerpairs=['Primer pair 1','Primer pair 2','Primer pair 3','Primer pair 4','Primer pair 5']

product_list=['Forward primer','Reverse primer','Product length']

for primer in list_primerpairs:

    print(primer)

    print("===========================================")

    for item in product_list:


        print(WebDriverWait(driver,15).until(EC.visibility_of_element_located((By.XPATH,"//h2[text()='{}']/following::table[1]//th[text()='{}']/following-sibling::td[1]".format(primer,item)))).text)

    print("==========================================")

输出:


Primer pair 1

===========================================

GAGGGCATACCCCTCGTAGA

CTGCCGTTTTCCGTAGGACT

506

==========================================

Primer pair 2

===========================================

AGTCCTACGGAAAACGGCAG

GCTATTCTCGCAGCTCACCA

621

==========================================

Primer pair 3

===========================================

AGGTAGTCAGTCAGGTCCCG

TGCCGTTTTCCGTAGGACTC

558

==========================================

Primer pair 4

===========================================

AGGGCATACCCCTCGTAGAT

TCTGCCGTTTTCCGTAGGAC

506

==========================================

Primer pair 5

===========================================

GGGCATACCCCTCGTAGATG

GCCGTTTTCCGTAGGACTCT

502

==========================================

希望这可以帮助。


查看完整回答
反对 回复 2023-10-24
?
BIG阳

TA贡献1859条经验 获得超6个赞

尝试以下代码:


driver.get('https://www.ncbi.nlm.nih.gov/tools/primer-blast/primertool.cgi?ctg_time=1585700551&job_key=9P4rCho2F54woA2lAMUpl3reOKVXzSO4Vg&CheckStatus=Check')


str1 = "Primer pair 1";

str2 = "Forward primer";

el = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='" +str1 +"']//following-sibling::table//th[text()='" +str2+"']//following-sibling::td[1]")))

print(el.text)

您可以更改字符串值。


出现此错误TypeError: 'str' object is not callable是因为您在这一行中出错:


pair_1 = driver.find_element(By.XPATH("the value"))

它应该:


pair_1 = driver.find_element(By.XPATH, "the value")


查看完整回答
反对 回复 2023-10-24
  • 3 回答
  • 0 关注
  • 126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信