【问题标题】:scraping the text from source code using python使用python从源代码中抓取文本
【发布时间】:2017-12-23 17:43:42
【问题描述】:

我正在尝试使用 python 和 selenium 抓取谷歌搜索结果。我只能得到第一个搜索结果。这是我正在使用的代码。

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

我怎样才能得到所有的搜索结果?

【问题讨论】:

  • 您尝试过什么以获得多个结果?
  • 我采用了Andersson提出的方案。

标签: python-2.7 selenium web-scraping


【解决方案1】:

尝试获取链接列表(仅从第一页。如果您需要抓取更多页面,您需要在循环中单击“下一步”按钮并附加后续页面的结果)如下:

href = [link.get_attribute("href") for link in driver.find_elements_by_css_selector('div.g a')]

附:您也可以使用来自this question 的解决方案来获取结果,作为带有requests lib 的GET 请求响应

【讨论】:

    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多