125 lines
4.2 KiB
Python
125 lines
4.2 KiB
Python
import scrapy
|
|
from configparser import ConfigParser
|
|
import os
|
|
import pandas as pd
|
|
|
|
|
|
|
|
class ProjectLinksSpider(scrapy.Spider):
|
|
name = 'links'
|
|
|
|
def __init__(self, website_name=None, start_urls=None, config_path=None, allowed_domains=None, *args, **kwargs):
|
|
super(ProjectLinksSpider, self).__init__(*args, **kwargs)
|
|
self.config = ConfigParser()
|
|
self.config_path = config_path
|
|
self.website_name = website_name
|
|
self.allowed_domains = allowed_domains.split(',') if allowed_domains else []
|
|
self.start_urls = start_urls
|
|
if config_path:
|
|
with open(os.getcwd() + config_path, 'r') as f:
|
|
self.config.read_file(f)
|
|
self.project_links = []
|
|
|
|
def start_requests(self):
|
|
self.xpath = dict(self.config.items(self.website_name))
|
|
for url in self.start_urls:
|
|
yield scrapy.Request(url, callback=self.parse, meta={'xpath': self.xpath}, dont_filter=True)
|
|
|
|
|
|
def parse(self, response):
|
|
xpath = response.meta.get('xpath', {})
|
|
# Extract links
|
|
for project in response.xpath(xpath['list']):
|
|
link = response.urljoin(project.xpath(xpath['links']).get())
|
|
yield{
|
|
'link': link
|
|
}
|
|
self.project_links.append(link)
|
|
|
|
# Handle pagination
|
|
next_page = response.xpath(xpath['next'])
|
|
if next_page is not None:
|
|
next_url = response.urljoin(next_page.get())
|
|
if next_url != response.url:
|
|
self.logger.info(f"Following next page: {next_url}")
|
|
yield scrapy.Request(url=next_url, callback=self.parse, dont_filter=True, meta={'xpath': self.xpath})
|
|
else:
|
|
self.save_links()
|
|
|
|
def save_links(self):
|
|
df = pd.DataFrame(self.project_links, columns=['project_link'])
|
|
df.to_json(f'project_links_{self.website_name}.json', orient='records')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# import scrapy
|
|
# from configparser import ConfigParser
|
|
# import os
|
|
# import pandas as pd
|
|
# import time
|
|
# import logging
|
|
|
|
|
|
# class ProjectLinksSpider(scrapy.Spider):
|
|
# name = 'links'
|
|
|
|
# def __init__(self, start_urls='https://gepris.dfg.de/gepris/OCTOPUS?context=projekt&findButton=historyCall&hitsPerPage=125&index=0&keywords_criterion=Universit%C3%A4t+Duisburg-Essen&nurProjekteMitAB=false&orderBy=relevance&task=doSearchSimple', website_name='DFG', config_link='./config/links_config.ini', allowed_domains='gepris.dfg.de'):
|
|
# super(ProjectLinksSpider, self).__init__()
|
|
# self.config_link = config_link
|
|
# self.config = ConfigParser()
|
|
# with open(os.getcwd() + self.config_link, 'r') as f :
|
|
# self.config.read_file(f)
|
|
# self.website_name = website_name
|
|
# self.allowed_domains = allowed_domains
|
|
# self.start_urls = start_urls
|
|
# self.links = []
|
|
|
|
# def start_requests(self):
|
|
# links_config = dict(self.config.items(self.website_name))
|
|
# yield scrapy.Request(
|
|
# url=self.start_urls,
|
|
# meta={'links_config':links_config},
|
|
# callback=self.parse,
|
|
# )
|
|
|
|
|
|
# def parse(self, response):
|
|
# # Handle link Extraction
|
|
# time.sleep(5)
|
|
# try:
|
|
# links_config = response.meta['links_config']
|
|
# project_list = response.xpath(links_config['projects-list'])
|
|
# for link in project_list:
|
|
# scraped_link = response.urljoin(link.xpath(links_config['project-link']).extract())
|
|
# yield{
|
|
# 'project_link': scraped_link
|
|
# }
|
|
# self.links.append(scraped_link)
|
|
# except:
|
|
# logging.warning('error is here')
|
|
|
|
# # Handle Pagination
|
|
# next_btn = response.xpath(links_config['next-btn']).get()
|
|
# if next_btn:
|
|
# next_url = response.urljoin(next_btn)
|
|
# yield response.follow(url=next_url, callback=self.parse, meta={'links_config': links_config})
|
|
# else:
|
|
# self.save_project_links()
|
|
|
|
# def save_project_links(self):
|
|
# df = pd.DataFrame(self.links)
|
|
# df.to_json(f'project_links_{self.website_name}.json', orient='records')
|
|
# return self.links
|