Files
generic-project-crawler/main.py
T

159 lines
5.7 KiB
Python

import requests
from scrapy.crawler import CrawlerProcess
import argparse
import json
from scrapyscript.spiders.project_links import ProjectLinksSpider
from seleniumscript.project_links import ProjectLinksSelenium
from seleniumscript.project_data import ScrapeWithSelenium
from scrapyscript.spiders.project_data import ProjectDataSpider
def main(start_urls):
def is_dynamic():
""" Inspects the HTML markup of provided start_url, returns True if the website is (dynamic) js-based
and False if it's (static) not js-based """
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'}
response = requests.get(url=start_urls, headers=headers)
if '<noscript' in response.text:
print('The website is JavaScript-based')
return True
elif input('Do you have to fill out a form to access the project links? (y/n): ').lower() == 'y':
print('The website is JavaScript-based')
return True
else:
print('The website is not JavaScript-based')
return False
def scrape_data_dynamic(project_links, website_name):
""" The scraped project url list from scrape_links_dynamic() is sent over
to ScrapeWithSelenium class along with other necessary arguments which the user must provide in console if the website is js_based """
body_wait_time = int(input("Enter the amount of time you want to wait for each page to load in seconds: "))
xpath_wait_time = int(input("Enter the amount of time you want to wait for each field to load in seconds: "))
config_xpath = input("Enter the path to your xpath configuration file: ")
scraper = ScrapeWithSelenium(urls=project_links, website_name=website_name, body_wait_time=body_wait_time, xpath_wait_time=xpath_wait_time, config_xpath=config_xpath)
scraper.run()
def scrape_links_dynamic(start_urls, website_name, config_link):
""" If is_dynamic() evalutes True, scrape_links_dynamic() is called.
The user must provide the neceessary arguments such as website_name and config_link
in console so that the scrape_links_dynamic can scrape the dynamic websites's links by instantiating ProjectLinksSelenium class """
body_wait_time = int(input("Enter the amount of time you want to wait for each page to load in seconds: "))
xpath_wait_time = int(input("Enter the amount of time you want to wait for each field to load in seconds: "))
ProjectLinksSelenium(start_urls=start_urls,
website_name=website_name,
config_link=config_link,
body_wait_time=body_wait_time,
xpath_wait_time=xpath_wait_time)
links = stored_links(website_name)
start_urls = [link['project_link'] for link in links]
scrape_data_dynamic(start_urls,website_name)
def scrape_data_static(project_links, website_name, allowed_domains):
""" The scraped project url list from scrape_links_static() is sent over
to ProjectDataSpider class along with other necessary arguments which the user must provide in console if the website is js_based """
config_xpath = input("Enter the path to your xpath configuration file: ")
process = CrawlerProcess()
process.crawl(ProjectDataSpider,
start_urls=project_links,
config_xpath=config_xpath,
website_name=website_name,
allowed_domains=allowed_domains)
process.start()
def scrape_links_static(start_urls,config_path, website_name, allowed_domains):
""" If is_dynamic() evalutes False, scrape_links_static() is called.
The user must provide the neceessary arguments such as website_name, config_link and allowed_domains
in console so that scrape_links_static can scrape the dynamic websites's links by instantiating ProjectLinksSpider class """
process = CrawlerProcess()
process.crawl(ProjectLinksSpider,
start_urls=start_urls,
config_path=config_path,
website_name=website_name,
allowed_domains=allowed_domains)
links = stored_links(website_name)
start_urls = [link['project_link'] for link in links]
scrape_data_static(project_links=start_urls, website_name=website_name, allowed_domains=allowed_domains)
def stored_links(name):
try:
with open(f'./project_links_{name}.json', 'r', encoding='utf-8') as f:
jsonlinks = json.load(f)
return jsonlinks
except FileNotFoundError:
print('File not found')
if is_dynamic():
website_name = input("Enter website name: ")
config_link = input("Enter path to links_config.ini: ")
scrape_links_dynamic(start_urls, website_name, config_link)
else:
website_name = input("Enter website name: ")
config_path = input("Enter the path to links_config.ini: ")
allowed_domains = input("Enter allowed domains: ")
scrape_links_static(start_urls=start_urls,config_path=config_path, website_name=website_name, allowed_domains=allowed_domains)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='The loading time needed for each page and field')
parser.add_argument('--start-urls', type=str, help='the start URL to scrape')
args = parser.parse_args()
main(start_urls=args.start_urls)