Advertisement
UF6

Google Search Scraper

UF6
Nov 22nd, 2023
1,594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | Source Code | 0 0
  1. from googlesearch import search
  2. import requests
  3. from bs4 import BeautifulSoup
  4.  
  5. def search_google_for_phrase(query, target_phrase):
  6.     try:
  7.         # Perform a Google search for the query
  8.         search_results = search(query, num=10, stop=10, pause=2)  # Adjust the number of results if needed
  9.  
  10.         # Iterate through the search results
  11.         for idx, result in enumerate(search_results, start=1):
  12.             try:
  13.                 # Fetch the content of the search result page
  14.                 response = requests.get(result)
  15.                 if response.status_code == 200:
  16.                     # Parse the HTML content of the page using BeautifulSoup
  17.                     soup = BeautifulSoup(response.text, 'html.parser')
  18.                     page_text = soup.get_text()
  19.  
  20.                     # Check if the target phrase exists in the page text
  21.                     if target_phrase in page_text:
  22.                         print(f"Found '{target_phrase}' on result {idx}: {result}")
  23.                     else:
  24.                         print(f"'{target_phrase}' not found on result {idx}: {result}")
  25.  
  26.                 else:
  27.                     print(f"Failed to fetch {result}. Status code: {response.status_code}")
  28.  
  29.             except requests.RequestException as e:
  30.                 print(f"Error fetching result {idx}: {e}")
  31.  
  32.     except Exception as e:
  33.         print(f"Error: {e}")
  34.  
  35. # Example usage:
  36. search_query = "Python programming"  # Replace with your search query
  37. phrase_to_find = "Python is versatile"  # Replace with the phrase you want to search for
  38.  
  39. search_google_for_phrase(search_query, phrase_to_find)
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement