Files
2023-05-11 09:31:31 +02:00

104 lines
4.9 KiB
Python

import requests
from scrapy.crawler import CrawlerProcess
from twisted.internet import defer
import argparse
import json
import logging
from requests.exceptions import MissingSchema
from scrapyscript.spiders.project_links import ProjectLinksSpider
from seleniumscript.project_links import ProjectLinksSelenium
from seleniumscript.project_data import ProjectDataSelenium
from scrapyscript.spiders.project_data import ProjectDataSpider
def main(start_urls):
def is_dynamic():
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 input('Do you have to fill out an HTML form to access the project links?(y/n): ').lower() == 'y' or input('Does the search criteria appear in the URL as a parameter?(y/n): ').lower() == 'n' :
print('Framework uses Selenium to scrape project calls')
return True
elif input('Does the website load the entire page at once without delay?(y/n): ').lower() == 'n':
if '<noscript' in response.text:
print('Framework uses Selenium to scrape project calls')
return True
else:
print('Framework uses Scrapy to scrape project calls')
return False
def scrape_data_dynamic(project_links, website_name):
logging.info('Scraping project data...')
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: ")
ProjectDataSelenium(urls=project_links,
website_name=website_name,
body_wait_time=body_wait_time,
xpath_wait_time=xpath_wait_time,
config_xpath=config_xpath)
def scrape_links_dynamic(start_urls, website_name):
config_path = input("Enter path to links_config.ini: ")
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: "))
logging.info('Scraping project links...')
ProjectLinksSelenium(start_urls=start_urls,
website_name=website_name,
config_path=config_path,
body_wait_time=body_wait_time,
xpath_wait_time=xpath_wait_time)
links = access_stored_links(website_name)
start_urls = [link['project_link'] for link in links]
scrape_data_dynamic(start_urls,website_name)
def crawl_static_website(website_name, start_urls, allowed_domains):
process = CrawlerProcess()
@defer.inlineCallbacks
def scrape_links_static():
config_path = input('Please enter the path to the links configuration file (links_config.ini): ')
yield process.crawl(ProjectLinksSpider, website_name=website_name, start_urls=start_urls, config_path=config_path, allowed_domains=allowed_domains)
yield scrape_data_static()
@defer.inlineCallbacks
def scrape_data_static():
config_xpath = input('Please enter the path to the field configuration file (websitename_config.ini): ')
jsonlinks = access_stored_links(website_name)
links = [link['project_link'] for link in jsonlinks]
yield process.crawl(ProjectDataSpider, website_name=website_name, start_urls=links, config_xpath=config_xpath, allowed_domains=allowed_domains)
scrape_links_static()
process.start()
def access_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: ")
scrape_links_dynamic(start_urls, website_name)
else:
website_name = input("Enter website name: ")
allowed_domains = input("Enter allowed domains: ")
crawl_static_website(website_name=website_name, start_urls=start_urls, allowed_domains=allowed_domains)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--start-urls', type=str, help='The start URL of the website to scrape')
args = parser.parse_args()
try:
main(start_urls=args.start_urls)
except (MissingSchema):
logging.error('Please provide the start URL of the website to scrape as string. Example: python main.py --start-urls "https://www.example.com"')