【问题标题】:Searching in Google with Python使用 Python 在 Google 中搜索
【发布时间】:2016-12-02 18:57:25
【问题描述】:

我想使用 python 脚本在 Google 中搜索文本并返回每个结果的名称、描述和 URL。我目前正在使用此代码:

from google import search

ip=raw_input("What would you like to search for? ")

for url in search(ip, stop=20):
     print(url)

这仅返回 URL。如何返回每个 URL 的名称和描述?

【问题讨论】:

  • 您使用了哪个谷歌搜索 API?

标签: python python-2.7 google-search


【解决方案1】:

不完全是我想要的,但我发现自己现在是一个不错的解决方案(如果我能做得更好,我可能会编辑它)。我像以前一样在 Google 中搜索(仅返回 URL)和用于解析 HTML 页面的 Beautiful Soup 包:

from googlesearch import search
import urllib
from bs4 import BeautifulSoup

def google_scrape(url):
    thepage = urllib.urlopen(url)
    soup = BeautifulSoup(thepage, "html.parser")
    return soup.title.text

i = 1
query = 'search this'
for url in search(query, stop=10):
    a = google_scrape(url)
    print str(i) + ". " + a
    print url
    print " "
    i += 1

这给了我页面标题和链接的列表。

还有另一个很棒的解决方案:

from googlesearch import search
import requests

for url in search(ip, stop=10):
            r = requests.get(url)
            title = everything_between(r.text, '<title>', '</title>')

【讨论】:

  • ImportError: cannot import name 'search'
  • @pyd 我来不及回答:D Tyr from googlesearch import search 使用 'googlesearch' 而不是 'google' ;)
  • 这段代码有错误,无效的sintax。检查这个:snipboard.io/M9q7Kg.jpg
【解决方案2】:

您可以使用 Google Search Origin 软件包,该软件包集成了 google 上可用的大部分参数(它包括 dorks 和过滤器)。它基于谷歌官方文档。此外,使用将创建一个对象,因此它很容易修改。有关更多信息,请查看此处的项目:https://pypi.org/project/google-search-origin/

这里是一个如何使用它的例子:

import google_search_origin


if __name__ == '__main__':
    # Initialisation of the class
    google_search = google_search_origin.GoogleSearchOrigin(search='sun')
    
    # Request from the url assembled
    google_search.request_url()

    # Display the link parsed depending on the result
    print(google_search.get_all_links())

    # Modify the parameter
    google_search.parameter_search('dog')

    # Assemble the url
    google_search.assemble_url()

    # Request from the url assembled
    google_search.request_url()

    # Display the raw text depending on the result
    print(google_search.get_response_text())

【讨论】:

    【解决方案3】:

    通常,您不能通过在 python3 中导入 google 包来使用 python 中的 google 搜索功能。但是你可以在python2中使用它。

    即使使用 requests.get(url+query),scraping 也不会执行,因为 google 通过将其重定向到验证码页面来防止抓取。

    可能的方法:

    • 可以用python2写代码
    • 如果你想用python3写,那就制作2个文件,从python2脚本中检索搜索结果。
    • 如果发现困难,最好的方法是使用带有 python3 运行时的 Google Colab 或 Jupyter Notebook。您不会收到任何错误。

    【讨论】:

      【解决方案4】:

      您还可以使用第三方服务,例如 Serp API,它是 Google 搜索引擎的结果。它解决了必须租用代理和解析 HTML 结果的问题。 JSON 输出特别丰富。

      很容易与 Python 集成:

      from lib.google_search_results import GoogleSearchResults
      
      params = {
          "q" : "Coffee",
          "location" : "Austin, Texas, United States",
          "hl" : "en",
          "gl" : "us",
          "google_domain" : "google.com",
          "api_key" : "demo",
      }
      
      query = GoogleSearchResults(params)
      dictionary_results = query.get_dictionary()
      

      GitHub:https://github.com/serpapi/google-search-results-python


      【讨论】:

      • 不幸的是,我猜他们只有付费版本。试用版也需要信用卡。
      【解决方案5】:

      我尝试使用其中的大多数,但对我没有用,或者尽管导入了包,但仍会出现诸如找不到搜索模块之类的错误。或者我确实使用了 selenium web driver,如果使用 Firefoxchrome 效果很好Phantom 网络浏览器,但我还是觉得执行时间有点慢,因为它先查询浏览器,然后返回搜索结果。

      所以我想到了使用 google api,它的运行速度惊人,而且返回结果准确

      在我分享代码之前,这里有一些快速提示:-

      1. 在 Google Api 上注册以获取 Google Api 密钥(免费版)
      2. 现在搜索 Google 自定义搜索并设置您的免费帐户以获得自定义搜索 ID
      3. 现在在你的 python 项目中添加这个包(google-api-python-client) (可以通过编写 !pip install google-api-python-client 来完成)

      就是这样,你现在要做的就是运行这段代码:-

      from googleapiclient.discovery import build
      
      my_api_key = "your API KEY TYPE HERE"
      my_cse_id = "YOUR CUSTOM SEARCH ENGINE ID TYPE HERE"
      
      def google_search(search_term, api_key, cse_id, **kwargs):
            service = build("customsearch", "v1", developerKey=api_key)
            res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
            return res['items']
      
      results= google_search("YOUR SEARCH QUERY HERE",my_api_key,my_cse_id,num=10) 
      
      for result in results:
            print(result["link"])
      

      【讨论】:

      • 能否提供google api python 客户端文档的链接?
      • 这是一个很好的解决方案,但仅供内部使用。对于企业解决方案,成本很高:)
      • 我非常想使用您的解决方案。但似乎在设置自定义搜索 ID 时,它特定于特定站点,例如“www.myownsite.com”。它并不适用于谷歌的所有结果。
      【解决方案6】:

      我假设您使用的是this library by Mario Vilas,因为stop=20 参数出现在他的代码中。看起来这个库除了 URL 之外什么都不能返回,这使得它非常不发达。因此,您当前使用的库无法实现您想要做的事情。

      我建议您改用abenassi/Google-Search-API。然后你可以简单地做:

      from google import google
      num_page = 3
      search_results = google.search("This is my query", num_page)
      for result in search_results:
          print(result.description)
      

      【讨论】:

      • 我得到:回溯(最近一次通话最后一次):文件“Z:/test/test_google.py”,第 57 行,在 from google import google ImportError: cannot import name google
      • @Yarden 您必须先下载库。使用链接中的说明。
      • 这非常有效。我最初遇到问题是因为我没有注意到 python-2.7 标记并试图在 Python 3 中安装该库。在 Python 2 中安装后,它完全符合我的需要。
      • 嗨,我收到了这个错误:'str' object has no attribute 'description'当我试图打电话给print(result.description)。我能做些什么吗?我运行完全相同的代码...
      • Google 似乎会阻止您使用此解决方案
      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      相关资源
      最近更新 更多