Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Created by .ignore support plugin (hsz.mobi)
### JupyterNotebooks template
# gitignore template for Jupyter Notebooks
# website: http://jupyter.org/

.ipynb_checkpoints
*/.ipynb_checkpoints/*

# IPython
profile_default/
ipython_config.py

# Remove previous ipynb_checkpoints
# git rm -r .ipynb_checkpoints/

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### VirtualEnv template
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
.Python
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
.venv
pip-selfcheck.json

### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
out
gen
### VisualStudioCode template
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Local History for Visual Studio Code
.history/

30 changes: 30 additions & 0 deletions INSTRUCOES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Desafio Webcrawler BIT

## Configurações iniciais:

- Certificar de que possui o python 3.
- O MongoDB estou utilizando o MongoDB Atlas que é em nuvem, portanto, o link para poder utiilzar via Shell/MongoDB
Compass ou alguma IDE que permita que faça consultas em banco é esse: <br>
``mongodb+srv://admin:<password>@cluster0.lw17c.mongodb.net/<dbname>?retryWrites=true&w=majority
`` <br>
OBS: Trocar as tags `<password>` e `<dbname>` para conseguir ter plena utilização do banco de dados.

- Caso ainda não tenha o PyMongo, DNSPython ou o Scrapy, na pasta raíz tem o arquivo `requirements.txt` que irá fazer a
instalação dos pacotes principais para rodar o robô. No CMD você pode digitar: <br>
``pip install -r requirements.txt``

## Como rodar:

Entrar na pasta WebCrawling/spiders e executar o comando no CMD:<br>
``scrapy crawl quotes``

O robô irá fazer a varredura de todas as citações — página por página — e irá guardar esses dados no banco `quotescrape`
na collection `marcosvinicius_simoescampos`.

## Queries:

Todas as queries que foram pedidas estão no arquivo `queries.js`. Lá tem os comentários sobre o retorno esperado. Tudo
que precisa ser feito é copiar e colar na ferramenta de consulta para verificar os dados que foram estruturados conforme
foi solicitado.

#### OBS: O arquivo ``items.json`` que está na pasta raíz foi criado para que veja como está estruturado os dados.
Empty file added WebCrawling/__init__.py
Empty file.
103 changes: 103 additions & 0 deletions WebCrawling/middlewares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals

# useful for handling different item types with a single interface
from itemadapter import is_item, ItemAdapter


class WebcrawlingSpiderMiddleware:
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the spider middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_spider_input(self, response, spider):
# Called for each response that goes through the spider
# middleware and into the spider.

# Should return None or raise an exception.
return None

def process_spider_output(self, response, result, spider):
# Called with the results returned from the Spider, after
# it has processed the response.

# Must return an iterable of Request, or item objects.
for i in result:
yield i

def process_spider_exception(self, response, exception, spider):
# Called when a spider or process_spider_input() method
# (from other spider middleware) raises an exception.

# Should return either None or an iterable of Request or item objects.
pass

def process_start_requests(self, start_requests, spider):
# Called with the start requests of the spider, and works
# similarly to the process_spider_output() method, except
# that it doesn’t have a response associated.

# Must return only requests (not items).
for r in start_requests:
yield r

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)


class WebcrawlingDownloaderMiddleware:
# Not all methods need to be defined. If a method is not defined,
# scrapy acts as if the downloader middleware does not modify the
# passed objects.

@classmethod
def from_crawler(cls, crawler):
# This method is used by Scrapy to create your spiders.
s = cls()
crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
return s

def process_request(self, request, spider):
# Called for each request that goes through the downloader
# middleware.

# Must either:
# - return None: continue processing this request
# - or return a Response object
# - or return a Request object
# - or raise IgnoreRequest: process_exception() methods of
# installed downloader middleware will be called
return None

def process_response(self, request, response, spider):
# Called with the response returned from the downloader.

# Must either;
# - return a Response object
# - return a Request object
# - or raise IgnoreRequest
return response

def process_exception(self, request, exception, spider):
# Called when a download handler or a process_request()
# (from other downloader middleware) raises an exception.

# Must either:
# - return None: continue processing this exception
# - return a Response object: stops process_exception() chain
# - return a Request object: stops process_exception() chain
pass

def spider_opened(self, spider):
spider.logger.info('Spider opened: %s' % spider.name)
Empty file added WebCrawling/models/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions WebCrawling/models/items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scrapy


class WebcrawlingItem(scrapy.Item):
title = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
17 changes: 17 additions & 0 deletions WebCrawling/pipelines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pymongo

MONGO_URL = 'mongodb+srv://admin:quotescrape@cluster0.lw17c.mongodb.net/quotescrape?retryWrites=true&w=majority'
MONGO_DB = 'quotescrape'
DB_COLLECTION = 'marcosvinicius_simoescampos'


class WebcrawlingPipeline:

def __init__(self):
self.connection = pymongo.MongoClient(MONGO_URL)
DATABASE = self.connection[MONGO_DB]
self.collection = DATABASE[DB_COLLECTION]

def process_item(self, item, spider):
self.collection.insert(dict(item))
return item
87 changes: 87 additions & 0 deletions WebCrawling/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Scrapy settings for WebCrawling project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'WebCrawling'

SPIDER_MODULES = ['WebCrawling.spiders']
NEWSPIDER_MODULE = 'WebCrawling.spiders'

# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'WebCrawling (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
# COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
# DEFAULT_REQUEST_HEADERS = {
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
# }

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
# 'WebCrawling.middlewares.WebcrawlingSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# DOWNLOADER_MIDDLEWARES = {
# 'WebCrawling.middlewares.WebcrawlingDownloaderMiddleware': 543,
# }

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'WebCrawling.pipelines.WebcrawlingPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
27 changes: 27 additions & 0 deletions WebCrawling/spiders/QuotesSpider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import scrapy
from WebCrawling.models.items import WebcrawlingItem


class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = ['http://quotes.toscrape.com']

def parse(self, response, **kwargs):
for quote in response.css('div.quote'):
title = quote.css('span.text::text').get()
author = {
'name': quote.css('small.author::text').get(),
'url': 'http://quotes.toscrape.com' + quote.css('span a::attr(href)').get()
}
tags = quote.css('div.tags a.tag::text').getall()

items = WebcrawlingItem()
items['title'] = title
items['author'] = author
items['tags'] = tags

yield items

next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
Empty file added WebCrawling/spiders/__init__.py
Empty file.
Loading