0
from urllib import response
import requests
import urllib
import pandas as pd
from requests_html import HTML
from requests_html import HTMLSession

def get_source(url):
    """Return the source code for the provided URL. 

    Args: 
        url (string): URL of the page to scrape.

    Returns:
        response (object): HTTP response object from requests_html. 
    """

    try:
        session = HTMLSession()
        response = session.get(url)
        return response

    except requests.exceptions.RequestException as e:
        print(e)
        
def scrape_google(query):

    query = urllib.parse.quote_plus(query)
    response = get_source("https://www.google.com/search?q=" + query)

    links = list(response.html.absolute_links)
    google_domains = ('https://www.google.', 
                      'https://google.', 
                      'https://webcache.googleusercontent.', 
                      'http://webcache.googleusercontent.', 
                      'https://policies.google.',
                      'https://support.google.',
                      'https://maps.google.')

    for url in links[:]:
        if url.startswith(google_domains):
            links.remove(url)

    return links

def get_results(query):
    
    query = urllib.parse.quote_plus(query)
    response = get_source("https://www.google.co.uk/search?q=" + query)
    
    return response

def parse_results(response):
    
    css_identifier_result = ".tF2Cxc"
    css_identifier_title = "h3"
    css_identifier_link = ".yuRUbf a"
    css_identifier_text = ".VwiC3b"
    
    results = response.html.find(css_identifier_result)

    output = []
    
    for result in results:

        item = {
            'title': result.find(css_identifier_title, first=True).text,
            'link': result.find(css_identifier_link, first=True).attrs['href'],
            'text': result.find(css_identifier_text, first=True).text
        }
        
        output.append(item)
        
    return output

def google_search(query):
    response = get_results(query)
    return parse_results(response)

I would like to add a part in my code to change pages but i can't find a way ! Can someone help pls ?

Matiiss
  • 5,970
  • 2
  • 12
  • 29
Julien
  • 3
  • 3
  • don't scrape Google, use their API – Matiiss Jul 28 '22 at 10:23
  • Yes but I don't want to use google api – Julien Jul 28 '22 at 12:54
  • I don't think I listed that as an option, use Google's search engine API, it's also going to make it easier, you won't need to parse anything, just get values from a dictionary – Matiiss Jul 28 '22 at 13:32
  • 1
    Does this answer your question? [Searching in Google with Python](https://stackoverflow.com/questions/38635419/searching-in-google-with-python) Read the second comment on the question though, again, you should use their API – Matiiss Jul 28 '22 at 13:45
  • I recently ran into a question similar to yours. I'm attaching a link to my answer: https://stackoverflow.com/a/72938742/18597245 – Artur Chukhrai Aug 11 '22 at 17:53

0 Answers0