我之前为一个站点做了一个解析器,现在我想为另一个 https://999.md/ru/list/real-estate/apartments-and-rooms?view_type=photo 做同样的事情但我有一个问题。我想从以下代码中提取链接:
<ul class="ads-list-photo">
<li class="ads-list-photo-item">
<div class="ads-list-photo-item-thumb">
<a href="/ru/39854705">
我正在尝试使用 selenium webdriver 和以下代码来做到这一点:
driver.get(url)
driver.find_element_by_xpath('//*[@id="container"]/div/section/nav/ul/li[2]/a').click() # acces Imobiliare
driver.find_element_by_link_text("Apartamente şi camere").click()
parentElement = WebDriverWait(driver, 10)
parentElement = parentElement.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="js-pjax-container"]')))
elementList = parentElement.find_elements_by_tag_name("li") #Acces lista cu obiecte
links = []
for element in driver.find_elements_by_xpath('//*[@id="js-ads-container"]/ul/li[1]/div[1]'):
links.append(element.get_attribute('href'))
links
但我没有获得链接的 alit,而是获得了 None 值的列表。怎么解决?
最佳答案
我想你可以使用:
driver = webdriver.Firefox()
driver.get("https://999.md/ru/list/real-estate/apartments-and-rooms?view_type=photo")
links = driver.find_elements_by_xpath("//div[@class='ads-list-photo-item-thumb']//a")
for x in links:
print x.get_attribute('href')
输出:
https://999.md/ru/40057219
https://999.md/ru/22587357
https://999.md/ru/38650049
https://999.md/ru/17528096
https://999.md/ru/31246607
https://999.md/ru/12459330
https://999.md/ru/8449720
https://999.md/ru/34011321
...
关于python - 如何从 selenium webdriver 元素中提取链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43688020/