google search by google api in r or python

六眼飞鱼酱① 提交于 2020-06-03 05:10:19

问题


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.


回答1:


Disclosure: I work at SerpApi.


You can use google-search-results package to get data from Google Maps. Check a demo at Repl.it.

from serpapi.google_search_results import GoogleSearchResults

params = {
    "engine": "google",
    "q": "coffee"
}

client = GoogleSearchResults(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"
          ]
        }
      }
    },
    {
      "position": 3,
      "title": "coffee - Amazon.com",
      "link": "https://www.amazon.com/coffee/s?k=coffee",
      "displayed_link": "www.amazon.com › coffee › k=coffee",
      "thumbnail": null,
      "snippet": "Amazon.com: coffee.",
      "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:wfQ5Et9Ni-kJ:https://www.amazon.com/coffee/s%3Fk%3Dcoffee+&cd=6&hl=en&ct=clnk&gl=us"
    },
    {
      "position": 4,
      "title": "What is Coffee? - National Coffee Association",
      "link": "http://www.ncausa.org/about-coffee/what-is-coffee",
      "displayed_link": "www.ncausa.org › About Coffee › What is Coffee?",
      "thumbnail": null,
      "snippet": "Everyone recognizes a roasted coffee bean, but you might not recognize an actual coffee plant. White flowers on coffee tree. Coffee trees are pruned short to ...",
      "cached_page_link": "http://webcache.googleusercontent.com/search?q=cache:eukMDyc_TlwJ:www.ncausa.org/about-coffee/what-is-coffee+&cd=15&hl=en&ct=clnk&gl=us"
    },
    {
      "position": 5,
      "title": "House of Coffi - Order Food Online - 107 Photos & 58 Reviews ...",
      "link": "https://m.yelp.com/biz/house-of-coffi-dover",
      "displayed_link": "m.yelp.com › Food › Coffee & Tea",
      "thumbnail": null,
      "snippet": "The coffee is good and the service is friendly. And I love the turkey/apple/Brie sandwich. Hooooowever I ordered one this morning and when… Read More.",
      "rich_snippet": {
        "top": {
          "detected_extensions": {
            "rating": 4.5,
            "reviews": 58,
            "price": 10,
            "price_range_under": 10
          },
          "extensions": [
            "Rating: 4.5",
            "58 reviews",
            "Price range: Under $10"
          ]
        }
      }
    },
    {
      "position": 6,
      "title": "THE BEST 10 Coffee & Tea in Dover, DE - Last Updated ...",
      "link": "https://www.yelp.com/search?cflt=coffee&find_loc=Dover%2C+DE",
      "displayed_link": "www.yelp.com › Dover, DE › Food",
      "thumbnail": null,
      "snippet": "Best Coffee & Tea in Dover, DE - House of Coffi, The Cobblestone Cafe, Governor's Cafe, Mugs Coffee Bar, La Baguette, Bavarian Bakery & Deli, Tim Hortons, ..."
    },
    {
      "position": 7,
      "title": "Coffee: Benefits, nutrition, and risks - Medical News Today",
      "link": "https://www.medicalnewstoday.com/articles/270202",
      "displayed_link": "www.medicalnewstoday.com › articles",
      "thumbnail": null,
      "snippet": "Drinking coffee can do much more than provide an energy boost. Some scientific studies have suggested that coffee can also reduce the risk of several health ...",
      "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:kzmdGVh8xu0J:https://www.medicalnewstoday.com/articles/270202+&cd=28&hl=en&ct=clnk&gl=us"
    },
    {
      "position": 8,
      "title": "Home | The Coffee Bean & Tea Leaf",
      "link": "https://www.coffeebean.com/",
      "displayed_link": "www.coffeebean.com",
      "thumbnail": null,
      "snippet": "Learn More. Free Delivery The Coffee Bean & Tea Leaf Food and Beverages ... Rewards. Order Ahead and Mobile Pay using The Coffee Bean® Rewards app.",
      "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:WpQxSYo2c6AJ:https://www.coffeebean.com/+&cd=29&hl=en&ct=clnk&gl=us",
      "related_pages_link": "https://www.google.com/search?hl=en&gl=us&q=related:https://www.coffeebean.com/+Coffee&tbo=1&sa=X&ved=2ahUKEwjl7er0gLvoAhWRQN4KHRqmAQwQHzAcegQICRAF"
    },
    {
      "position": 9,
      "title": "Coffee beans | Peet's Coffee",
      "link": "https://www.peets.com/coffee",
      "displayed_link": "www.peets.com › coffee",
      "thumbnail": null,
      "snippet": "We start by sourcing the highest quality beans from farms around the world. Then we roast by hand to bring out the nuances in every cup, and deliver our coffee ...",
      "cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:M0kq3m8QPKYJ:https://www.peets.com/coffee+&cd=30&hl=en&ct=clnk&gl=us"
    }
  ]
}

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.




回答2:


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



来源:https://stackoverflow.com/questions/39453352/google-search-by-google-api-in-r-or-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!