Search Google with Python

I want to search text in Google using a python script and return the name, description and URL for each result. I am currently using this code:

from google import search

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

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

This returns only the URL. How can I return a name and description for each url?

+7
source share
4 answers

Not what I was looking for, but the other day I found a good solution (I could edit this if I can do it better). I combined a search on Google, like me (returning only the URL), and the Beautiful Soup package for parsing HTML pages:

from google 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

This gives me a list of page titles and links.

And another great solution:

from google import search
import requests

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

, - 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)
+11

, , , . - selenium, , Firefox chrome Phantom -, , , , .

google api, .

, : -

  • Google Api, Google Api ( )
  • Google Custom Search ,
  • (google-api-python-client) python ( ! 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"])
+2

, 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


0

Source: https://habr.com/ru/post/1649395/


All Articles