Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- import logging
- import sqlite3
- import re
- from pathlib import Path
- class GhostQuery:
- def __init__(self):
- self.base_url = "https://www.google.com/search?q="
- self.headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
- }
- self.log_filename = 'ghostquery.log'
- self.db_filename = 'ghostquery_results.db'
- self.logger = self.setup_logger()
- self.setup_database()
- def setup_logger(self):
- logger = logging.getLogger('GhostQuery')
- logger.setLevel(logging.INFO)
- formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
- file_handler = logging.FileHandler(self.log_filename)
- file_handler.setFormatter(formatter)
- logger.addHandler(file_handler)
- return logger
- def setup_database(self):
- db_path = Path(self.db_filename)
- if not db_path.is_file():
- conn = sqlite3.connect(self.db_filename)
- c = conn.cursor()
- c.execute('''CREATE TABLE IF NOT EXISTS search_results
- (title text, link text)''')
- conn.commit()
- conn.close()
- def save_to_database(self, title, link):
- conn = sqlite3.connect(self.db_filename)
- c = conn.cursor()
- c.execute("INSERT INTO search_results VALUES (?, ?)", (title, link))
- conn.commit()
- conn.close()
- def google_search(self, query, num_results=10):
- try:
- query = re.sub(r'[;\'"()|]', '', query)
- url = f"{self.base_url}{query}&num={num_results}"
- response = requests.get(url, headers=self.headers)
- if response.status_code == 200:
- soup = BeautifulSoup(response.text, 'html.parser')
- results = soup.find_all('div', class_='tF2Cxc')
- return results
- else:
- self.logger.error(f"Failed to retrieve search results. Status code: {response.status_code}")
- return None
- except requests.exceptions.RequestException as e:
- self.logger.error(f"Request error: {str(e)}")
- return None
- except Exception as e:
- self.logger.error(f"An unexpected error occurred: {str(e)}")
- return None
- def analyze_results(self, results):
- if results:
- analysis = []
- for idx, result in enumerate(results, start=1):
- title = result.find('h3').text
- link = result.find('a')['href']
- analysis.append({'index': idx, 'title': title, 'link': link})
- self.save_to_database(title, link)
- return analysis
- else:
- self.logger.warning("No results found for analysis.")
- return []
- def run(self):
- try:
- num_results = int(input("Enter the number of results to fetch (default is 10): ") or 10)
- promiscuous_mode = input("Enable Promiscuous Mode? (y/n): ").strip().lower()
- if promiscuous_mode == 'y':
- print("Promiscuous Mode is enabled.")
- else:
- print("Promiscuous Mode is disabled.")
- query = input("Enter your search query: ")
- results = self.google_search(query, num_results)
- analysis = self.analyze_results(results)
- if analysis:
- print("Analysis Results:")
- for item in analysis:
- print(f"{item['index']}. {item['title']}")
- print(item['link'])
- print()
- else:
- print("No results found. Check the log file for details.")
- except KeyboardInterrupt:
- print("\nSearch aborted by the user.")
- except Exception as e:
- self.logger.error(f"An unexpected error occurred: {str(e)}")
- if __name__ == "__main__":
- ghost_query = GhostQuery()
- ghost_query.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement