python - 如何使用 Selenium 从搜索结果中提取 Google 链接的 href?

标签 python selenium phantomjs

最终我只是想获取第一个链接的 href 到 google 的搜索结果

我需要的信息也存在于“a”元素中,但它存储在“data-href”属性中,我不知道如何从中提取数据(get_attribute('data-href') 返回 None)。

我正在使用 Phantomjs,但也尝试过使用 Firefox 网络驱动程序


href 显示在 cite 中谷歌搜索中的标签(可以通过检查谷歌搜索结果中每个链接下的绿色小链接文本找到)。

引用元素显然是在 Selenium 中找到的,但返回的文本( element.textget_attribute('innerHTML') 或( text ))不是 html 中显示的内容。

例如,有一个引用标签<cite class="_Rm">www.fcv.org.br/</cite> ,但是element.text显示“wikimapia.org/.../Fundação-Cristiano-Varella-Hospital...”

我试图用 by_css_selector 检索引用元素, tag_name , class_name , 和 xpath 具有相同的结果。

links = driver.find_elements_by_css_selector('div.g') # div[class="g"]
link = links[0] # I am looking for the first link in the main links section
next = link.find_element_by_css_selector('div[class="s"]') # location of cite tag
nextB = next.find_element_by_tag_name('cite') 

包含cite标签的div(div中只有一个)

    <div class="s">
         <div>
             <div class="f kv _SWb" style="white-space:nowrap">
                  <cite class="_Rm">www.fcv.org.br/</cite>

最佳答案

在每个搜索结果中找到第一个 a 元素并获取它的 href 属性值:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("https://www.google.com/search?q=test")

results = driver.find_elements_by_css_selector('div.g')
link = results[0].find_element_by_tag_name("a")
href = link.get_attribute("href")

然后你可以extract the actual url from the href value with urlparse :

import urlparse

print(urlparse.parse_qs(urlparse.urlparse(href).query)["q"])

打印:

[u'http://www.speedtest.net/']

关于python - 如何使用 Selenium 从搜索结果中提取 Google 链接的 href?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35241230/

相关文章:

php - 是否存在一个不错的 python wsgi 来本地管理 MYSQL 数据库?或者我必须自己编码一个?

python - 对于带有 Fastspring 的 Python 应用程序,我应该使用什么序列号许可解决方案?

java - 循环逻辑在 java selenium 中未按预期工作

node.js - 页脚的内容似乎不起作用

python - PhantomJS 看不到由 AJAX 填充的下拉选项

Python - 获取堆中小于 n 的最大数

python - 如何在 Selenium Python 中设置 UnexpectedAlertBehaviour

python - 我如何在 .send_keys() 中的字符之间进行延迟

javascript - nightwatch.js 互联网浏览器

phantomjs - 如何在 PhantomJS 中使用 ES6