【问题标题】:google search by google api in r or python在 r 或 python 中通过 google api 进行谷歌搜索
【发布时间】:2016-09-12 15:02:09
【问题描述】:

我想通过 python 或 R 在 google 中搜索一些东西(例如:“python 语言”),它会给我该 google 搜索的链接列表 喜欢:

https://en.wikipedia.org/wiki/Python_(programming_language)
https://www.python.org/
https://www.python.org/about/gettingstarted/

有没有相应的 api 我经历了这个问题 How to get google search results

但问题是有时它正在工作,而大多数时候它不工作并且只给出空 list() 作为输出。 谢谢。

【问题讨论】:

    标签: python r web-scraping google-api google-api-python-client


    【解决方案1】:

    免责声明:我在 SerpApi 工作

    您可以使用google-search-results 包从 Google 搜索中提取链接。

    Full example at Repl.it

    import os
    from serpapi import GoogleSearch
    
    params = {
        "engine": "google",
        "q": "coffee",
        "api_key": os.getenv("API_KEY")
    }
    
    client = GoogleSearch(params)
    data = client.get_dict()
    
    print("Organic results\n")
    
    for result in data['organic_results']:
      print(f"Link: {result['link']}")
    

    回应

    {
        "organic_results": [
        {
          "position": 1,
          "title": "Coffee - Wikipedia",
          "link": "https://en.wikipedia.org/wiki/Coffee",
          "displayed_link": "en.wikipedia.org › wiki › Coffee",
          "thumbnail": null,
          "snippet": "Coffee is a brewed drink prepared from roasted coffee beans, the seeds of berries from certain Coffea species.",
          "sitelinks": {
            "inline": [
              {
                "title": "History of coffee",
                "link": "https://en.wikipedia.org/wiki/History_of_coffee"
              },
              {
                "title": "Coffee bean",
                "link": "https://en.wikipedia.org/wiki/Coffee_bean"
              },
              {
                "title": "Coffee production",
                "link": "https://en.wikipedia.org/wiki/Coffee_production"
              },
              {
                "title": "Coffee preparation",
                "link": "https://en.wikipedia.org/wiki/Coffee_preparation"
              }
            ],
            "list": [
              {
                "date": "Color‎: ‎Black, dark brown, light brown, beige"
              }
            ]
          },
          "rich_snippet": {
            "bottom": {
              "detected_extensions": {
                "introduced‎_‎th_century": 15
              },
              "extensions": [
                "Introduced‎: ‎15th century",
                "Color‎: ‎Black, dark brown, light brown, beige"
              ]
            }
          },
          "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:U6oJMnF-eeUJ:https://en.wikipedia.org/wiki/Coffee+&cd=4&hl=en&ct=clnk&gl=us",
          "related_pages_link": "https://www.google.com/search?hl=en&gl=us&q=related:https://en.wikipedia.org/wiki/Coffee+Coffee&tbo=1&sa=X&ved=2ahUKEwjl7er0gLvoAhWRQN4KHRqmAQwQHzADegQIAhAH"
        },
        {
          "position": 2,
          "title": "The House of Coffi - Coffee Shop - Dover, Delaware - 132 ...",
          "link": "https://www.facebook.com/TheHouseOfCoffi/",
          "displayed_link": "www.facebook.com › Places › Dover, Delaware › Restaurant",
          "thumbnail": null,
          "snippet": "People talk about \"iced mocha coffee\", \"flavored iced tea\" and \"chai tea\". Relaxing atmosphere・Expert baristas・Convenient location. My first visit was a very ...",
          "rich_snippet": {
            "top": {
              "detected_extensions": {
                "rating": 5,
                "votes": 132
              },
              "extensions": [
                "Rating: 5",
                "132 votes"
              ]
            }
          }
        }
    
        // Stripped...
      ]
    }
    

    输出

    Organic results
    
    Link: https://en.wikipedia.org/wiki/Coffee
    Link: https://www.amazon.com/coffee/s?k=coffee
    Link: https://www.kansas.com/entertainment/restaurants/dining-with-denise-neil/article4383840.html
    Link: https://www.reverieroasters.com/
    Link: https://www.medicalnewstoday.com/articles/270202
    Link: http://www.ncausa.org/about-coffee/what-is-coffee
    Link: http://www.ncausa.org/about-coffee
    Link: https://www.facebook.com/KookaburraCoffeeLyfe/
    

    如果您想了解更多信息,请查看SerpApi documentation

    【讨论】:

      【解决方案2】:

      我用这个python web search api 在 python 中执行谷歌搜索。相当简单易用且易于安装。

      【讨论】:

        猜你喜欢
        • 2012-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-20
        相关资源
        最近更新 更多