Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from googlesearch import search
- import requests
- from bs4 import BeautifulSoup
- def search_google_for_phrase(query, target_phrase):
- try:
- # Perform a Google search for the query
- search_results = search(query, num=10, stop=10, pause=2) # Adjust the number of results if needed
- # Iterate through the search results
- for idx, result in enumerate(search_results, start=1):
- try:
- # Fetch the content of the search result page
- response = requests.get(result)
- if response.status_code == 200:
- # Parse the HTML content of the page using BeautifulSoup
- soup = BeautifulSoup(response.text, 'html.parser')
- page_text = soup.get_text()
- # Check if the target phrase exists in the page text
- if target_phrase in page_text:
- print(f"Found '{target_phrase}' on result {idx}: {result}")
- else:
- print(f"'{target_phrase}' not found on result {idx}: {result}")
- else:
- print(f"Failed to fetch {result}. Status code: {response.status_code}")
- except requests.RequestException as e:
- print(f"Error fetching result {idx}: {e}")
- except Exception as e:
- print(f"Error: {e}")
- # Example usage:
- search_query = "Python programming" # Replace with your search query
- phrase_to_find = "Python is versatile" # Replace with the phrase you want to search for
- search_google_for_phrase(search_query, phrase_to_find)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement