Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Spider de Caçapava-SP [Fixes #1131] #1274

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions data_collection/gazette/spiders/sp/sp_cacapava.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date, datetime
from urllib.parse import parse_qsl, urlencode, urlparse

from scrapy.http import Request

Expand All @@ -19,19 +20,18 @@ def start_requests(self):
url = f"https://cacapava.sp.gov.br/diario-oficial?dataDe={data_de}&dataAte={data_ate}"
yield Request(url)

def parse(self, response):
num_pages = int(
response.css(".pagination__label::text").re_first(r"\/ (\d+)") or "1"
)
if num_pages > 1:
for page in range(1, num_pages + 1):
yield Request(f"{response.url}&pagina={page}")
def pagination_url(self, url: str, page: str):
url_parsed = urlparse(url)
url_query = dict(parse_qsl(url_parsed.query))
url_query.update({"pagina": page})
return url_parsed._replace(query=urlencode(url_query)).geturl()

def parse(self, response):
for gazette in response.css(".list-item__info"):
edition_number = gazette.css("h3::text").re_first(r"Edição nº (\d+)")
gazette_raw_date = gazette.css("p::text").re_first(r"\d{2}/\d{2}/\d{4}")
gazette_date = datetime.strptime(gazette_raw_date, "%d/%m/%Y").date()
gazette_url = gazette.css("a::attr(href)")
gazette_url = gazette.css("a::attr(href)").get()

yield Gazette(
date=gazette_date,
Expand All @@ -40,3 +40,7 @@ def parse(self, response):
power="executive_legislative",
file_urls=[gazette_url],
)

pages = response.css(".pagination__select option::text").getall()
for page in pages[1:]:
yield Request(self.pagination_url(response.url, page))
Loading