gpt4 book ai didi

Python- Selenium : Scraping AngularJS elements with loop over find_elements_by()

转载 作者:太空宇宙 更新时间:2023-11-04 05:35:35 25 4
gpt4 key购买 nike

我正在抓取房地产数据。在使用 javascript Selenium 生成的站点上,Selenium 做了出色的工作:您找到包含相关信息的标签并使用

遍历所有这些标签
driver.find_elements_by...

但是在这个 site 上,列表是由 angular js 生成的。我尝试了同样的方法:

for article in driver.find_elements_by_css_selector("div.property.ng-scope"):
do something

我发现我必须让我的网络驱动程序 (phantomJS) 单击指向各个列表站点的链接:

linkbase = article.find_element_by_css_selector("div.info.clear.ng-scope")
link = linkbase.find_element_by_tag_name('a')
link.click()

然后 webdriver 直接指向该站点,我可以获得一个列表所需的所有信息。

一旦循环结束,我就会收到以下错误:

> Message: {"errorMessage":"Element does not exist in cache","request":{"headers":
{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","
Content-Length":"142","Content-Type":"application/json;charset=UTF-8","Host":"12
7.0.0.1:56577","User-Agent":"Python-urllib/3.4"},"httpVersion":"1.1","method":"P
OST","post":"{\"sessionId\": \"f9ec2c10-dfd9-11e5-9d4c-3bbe8f5bf7c0\", \"using\"
: \"css selector\", \"id\": \":wdc:1456856343349\", \"value\": \"div.info.clear.
ng-scope\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"elemen
t","directory":"/","path":"/element","relative":"/element","port":"","host":"","
password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/ele
ment","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/f9ec2c10-dfd9-
11e5-9d4c-3bbe8f5bf7c0/element/:wdc:1456856343349/element"}}

页面上包含链接的元素是:

<a ng-href="/detail/prodej/dum/rodinny/jemnice-jemnice-/3800125532" ng-click="beforeOpen(i.iterator, i.regionTip)" class="title" href="/detail/prodej/dum/rodinny/jemnice-jemnice-/3800125532">
<span class="name ng-binding"> ... </a>

这只是每个列表的标题文本。我确实在 this answer 之后设置了一个用户代理,即使它没有出现在错误中。我也在等待周围的元素被加载:

wait = WebDriverWait(driver, getSearchResults_CZ.waiting)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.content")))

我想要的是解析所有这些属性元素,将它们的链接保存到列表中,然后遍历列表,用driver.get() 打开每个链接我知道通过单击链接,驱动程序 url 发生了变化,但我认为一旦使用 find_elements_by 建立了文章列表,它将作为一个稳定的引用点。通过搜索“a”标签并调用 get_attribute('href') 来访问链接在这种情况下使用 angular js 框架不起作用。我没有看到什么?

编辑:正如所回答的,没有 .click() 的 get_attribute 是正确的方法。我最初的错误与 CSS 选择器有关:我一直在使用“div[class^='property']”并得到一个完全不同的链接。一定是找到了我以前没见过的另一个元素。

最佳答案

等待至少一个“属性”可见,然后获取链接:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://www.sreality.cz/hledani/prodej/domy?region=jemnice")
driver.maximize_window()

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "property")))

links = [link.get_attribute("href") for link in driver.find_elements_by_css_selector("div.property div.info a")]
print(links)

driver.close()

对我有用。

关于Python- Selenium : Scraping AngularJS elements with loop over find_elements_by(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com