如何抓取这个字段?

时间:2021-04-29 17:42:20

标签: python selenium web-scraping xpath

我的代码转到网页并抓取所有作者,但我也需要会话名称。

我当前的代码将只打印“SESSION”而不是会话的值

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait=WebDriverWait(driver, 70)

driver.get('https://meetinglibrary.asco.org/results?filters=JTVCJTdCJTIyZmllbGQlMjIlM0ElMjJmY3RNZWV0aW5nTmFtZSUyMiUyQyUyMnZhbHVlJTIyJTNBJTIyQVNDTyUyMEFubnVhbCUyME1lZXRpbmclMjIlMkMlMjJxdWVyeVZhbHVlJTIyJTNBJTIyQVNDTyUyMEFubnVhbCUyME1lZXRpbmclMjIlMkMlMjJjaGlsZHJlbiUyMiUzQSU1QiU1RCUyQyUyMmluZGV4JTIyJTNBMCUyQyUyMm5lc3RlZFBhdGglMjIlM0ElMjIwJTIyJTdEJTJDJTdCJTIyZmllbGQlMjIlM0ElMjJZZWFyJTIyJTJDJTIydmFsdWUlMjIlM0ElMjIyMDIxJTIyJTJDJTIycXVlcnlWYWx1ZSUyMiUzQSUyMjIwMjElMjIlMkMlMjJjaGlsZHJlbiUyMiUzQSU1QiU1RCUyQyUyMmluZGV4JTIyJTNBMSUyQyUyMm5lc3RlZFBhdGglMjIlM0ElMjIxJTIyJTdEJTVE')


productList=wait.until(EC.presence_of_all_elements_located((By.XPATH,"//div[@class='record']")))
for product in productList:
        author=product.find_element_by_xpath(".//div[@class='record__ellipsis']").text
        session=product.find_element_by_xpath("//div[@class='record__meta__title']").text
        print(session)

2 个答案:

答案 0 :(得分:1)

要获取会话名称,请使用 xpath:

//div[@class='record__meta ng-star-inserted'][1]/div[2]

要获取会话标头,我会使用:

//div[@class='record__category ng-star-inserted']

和标题:

//div[@class='record__title ng-star-inserted']/span

答案 1 :(得分:1)

对于会话尝试使用此 xpath .//div[@class='record__meta ng-star-inserted' and contains(., 'Session')]

session=product.find_element_by_xpath(".//div[@class='record__meta ng-star-inserted' and contains(., 'Session')]").text