【发布时间】:2021-03-03 06:25:05
【问题描述】:
网站中有一些具有不同 xpath 的元素,例如“代码”:
.td[1]/div[4]/div
.td[1]/div[3]/div
null
如果 xpath = .td[1]/div[4]/div,那么 .td[1]/div[3]/div 中应该有另一个 ISIN 代码 我想通过以下方式访问它们:
driver.get('https://www.chinabondconnect.com/en/Primary/Primary-Information/Onshore.html')
wait = WebDriverWait(driver, 30)
driver.find_element_by_link_text('Others').click()
try:
codes=[code.get_attribute('textContent') for code in driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[1]/div[4]/div")]
ISINs=[ISIN.get_attribute('textContent') for ISIN in driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[1]/div[3]/div")]
except:
try:
codes=[code.get_attribute('textContent') for code in driver.find_elements_by_xpath("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[1]/div[3]/div")]
except:
codes = 'null'
#dateframe=...
但它不会返回网站中的所有发行人,只有少数几个,不知道为什么会这样,任何帮助将不胜感激!
【问题讨论】:
-
只有 12 个 isin 和 53 个代码,你期待什么?
-
嗨,但我希望无值的可以在数据框中返回“null”,但它只是消除了没有代码的那些
-
所以你想让 isin 和代码长度相等吗?使用正常循环。
-
你想要全部 200 行左右吗?
-
@ArundeepChohan 你好,是的,我想获取所有记录的发行人的代码/ISIN 信息(如果没有,只需在 csv 数据框中返回“null”或空白)也可以用于到期xpath 不同
("//table[@id='tb7']//tr[starts-with(@class,'tb2tr pg')]//td[3]/div[3]/span[2]")或div[5],不知道如何处理这种情况..