A.Naskar
A.Naskar

Reputation: 49

google search by google api in r or python

I want to search some thing (ex:"python language") in google by python or R and it will give me the list of links for that google search like:

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

Is there any api for that I went through this question How to get google search results

but problem is sometime it is working and most of the time it is not working and giving only empty list() as an output. thanks.

Upvotes: 0

Views: 994

Answers (2)

ilyazub
ilyazub

Reputation: 1414

Disclaimer: I work at SerpApi.

You can use the google-search-results package to extract links from Google Search.

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']}")

Response

{
    "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...
  ]
}

Output

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/

If you want more information, check out SerpApi documentation.

Upvotes: 1

alpha34
alpha34

Reputation: 13

I have used this python web search api to perform google search in python. Fairly straightforward to use and easy to install.

Upvotes: 0

Related Questions