This project consists of three main Python scripts designed for different tasks involving web automation and data retrieval:
accessHistory.py: Retrieves browser history from a SQLite database.googleAutomate.py: Automates Google searches using Selenium WebDriver and scrapes links from search results.webcrawler.py: Crawls websites to extract valid links using requests and BeautifulSoup.
This script connects to a SQLite database containing browser history and retrieves URLs in order of their last visit time.
- Ensure you have the required SQLite database file (e.g.,
Historyfile from a Chrome browser profile). - Update the script with the correct path to the
Historyfile. - Run the script to fetch and print the URLs.
import sqlite3
con = sqlite3.connect("path_to_history_file")
cur = con.cursor()
res = cur.execute("SELECT url FROM urls ORDER BY last_visit_time")
print(res.fetchall())This script automates Google searches using Selenium WebDriver, reads search engines and keywords from files, and retrieves links from search results pages. It also integrates the web_crawler function from webcrawler.py to further crawl the retrieved links.
-
Install Selenium and Chrome WebDriver:
pip install selenium webdriver-manager
-
Create
search_engines.txtandkeywords.txtfiles containing search engine URLs and search keywords, respectively.
- Update the
pathvariable to the folder where Chrome user data will be stored. - Run the script to perform searches and print the retrieved links.
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from webcrawler import web_crawler
import random as rnd
def read_keywords_from_file(file_path):
with open(file_path, 'r') as file:
keywords = [line.strip() for line in file.readlines()]
return keywords
chrome_options = webdriver.ChromeOptions()
path = "path_to_chrome_user_data"
chrome_options.add_argument("--user-data-dir=" + path)
chrome_options.add_argument("--profile-directory=Default")
service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
search_engines = read_keywords_from_file("search_engines.txt")
query_list = read_keywords_from_file("keywords.txt")
links = []
rnd.shuffle(query_list)
for query in query_list:
query = query.replace(' ', '+')
n_pages = 3
for page in range(1, n_pages):
url = rnd.choice(search_engines) + query + "&start=" + str(page)
links.append(url)
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
search = soup.find_all('div', class_="yuRUbf")
for h in search:
links.append(h.a.get('href'))
links.extend(web_crawler(rnd.choice(links)))
links = set(links)
driver.quit()
if __name__ == '__main__':
print(links)
quit()This script contains a function web_crawler that takes a URL, sends a GET request, and extracts valid links from the webpage using BeautifulSoup.
- Install BeautifulSoup and requests:
pip install beautifulsoup4 requests
- Import the
web_crawlerfunction in your script. - Call
web_crawler(url)to crawl the given URL and retrieve valid links.
import requests
from bs4 import BeautifulSoup
def web_crawler(url):
reject_list = ["facebook.com", "linkedin.com", "instagram.com"]
link_list = []
try:
response = requests.get(url, timeout=10, allow_redirects=False)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
links = soup.find_all('a')
for link in links:
href = link.get('href')
if href and "https://www." in href and not any(x in href for x in reject_list):
link_list.append(href)
return set(link_list) if link_list else {'https://www.google.com'}
else:
return {'https://www.yahoo.com'}
except Exception as e:
print(f"Error occurred while crawling {url}: {e}")
return {'https://www.chess.com'}
def read_urls_from_file(file_path):
try:
with open(file_path, 'r') as file:
return [line.strip() for line in file.readlines()]
except FileNotFoundError:
print(f"Error: The file {file_path} was not found.")
return []
except Exception as e:
print(f"An error occurred while reading the file {file_path}: {e}")
return []
if __name__ == '__main__':
file_path = 'search_engines.txt'
urls = read_urls_from_file(file_path)
link_list = []
if urls:
for url in urls:
link_list.extend(web_crawler(url))
for link in set(link_list):
print(link)
else:
print("No URLs to process.")- Python 3.x
sqlite3(built-in with Python)requestslibrarybeautifulsoup4libraryseleniumlibrarywebdriver-managerlibrary
Install the necessary packages using:
pip install requests beautifulsoup4 selenium webdriver-manager- Ensure all required files (
search_engines.txt,keywords.txt, and the ChromeHistoryfile) are in place. - Execute each script individually based on the described usage.
This project is licensed under the MIT License.
The BrAVo Team!