Files
generic-project-crawler/Scrapy_or_Selenium.py
T
2023-02-27 11:48:38 +01:00

101 lines
1.7 KiB
Python

# -------------------------- Overview -------------------------- #
'''
In Scrapy_or_Selenium.py file we pass the first project link stored
In an already scraped project link file(Here: "project_links_BMBF.json", "project_links_EU.json" and "project_links_DFG.json").
In order to check if a website is JavaScript enabled or not, one should check if the website works as expected by disableing
JS with chrome inspection tools.
In order to make this test automatic, I examined the html markup to see if it contains <noscript></noscript> or not.
If yes (JS-based): all the project urls of that specific file will be sent to selenium.py located in selenium_script directory
If no (not JS-based) : all the project urls of that specific file will be sent to project_data.py located inside spiders directory.
'''
# --------------------------------------------------------------- #
import requests
import json
import subprocess
from selenium_script.selenium import ScrapeWithSelenium
with open('./Projectlinks/project_links_DFG.json', 'r', encoding='utf-8') as f:
jsonlinks = json.load(f)
test_link = jsonlinks[0]['project_link']
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'}
response = requests.get(url=test_link, headers=headers)
if '<noscript' in response.text:
print('The website is JavaScript-based')
urls = [link['project_link'] for link in jsonlinks]
scraper = ScrapeWithSelenium(urls)
scraper.run()
else:
print('The website is not JavaScript-based')
start_urls = [link['project_link'] for link in jsonlinks]
subprocess.call(['scrapy', 'crawl', 'project-data', '-a', 'start_urls=' + ','.join(start_urls)])