-
Notifications
You must be signed in to change notification settings - Fork 1
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
refactor: Simplify command line for scraping datasets #186
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import re | ||
from typing import Iterator, Optional | ||
|
||
import html2text | ||
import scrapy | ||
from scrapy.http import HtmlResponse | ||
|
||
|
||
class CaFranchiseTaxBoardSpider(scrapy.Spider): | ||
# This name is used on the commandline: scrapy crawl edd_spider | ||
name = "ca_ftb_spider" | ||
allowed_domains = ["www.ftb.ca.gov"] | ||
start_urls = ["https://www.ftb.ca.gov/file/personal/credits/index.html"] | ||
|
||
# This is used to substitute the base URL in the cache storage | ||
common_url_prefix = "https://www.ftb.ca.gov/file/" | ||
|
||
def parse(self, response: HtmlResponse) -> Iterator[scrapy.Request | dict[str, str]]: | ||
self.logger.info("Parsing %s", response.url) | ||
|
||
nav_links = response.css("nav.local-nav a") | ||
for link in nav_links: | ||
if "class" in link.attrib and link.attrib["class"] == "uplevel": | ||
# Skip the uplevel/back link that goes to the parent page | ||
continue | ||
|
||
assert link.attrib["href"] | ||
self.logger.info("Found nav link: %s", link) | ||
yield response.follow(link, callback=self.parse_childpage) | ||
|
||
yield self.parse_childpage(response) | ||
|
||
def parse_childpage(self, response: HtmlResponse) -> dict[str, str]: | ||
self.logger.info("Parsing %s", response.url) | ||
|
||
if (h1_count := len(response.css("h1").getall())) > 1: | ||
self.logger.warning("Found %i h1 elements for %r", h1_count, response.url) | ||
raise ValueError("Multiple h1 elements found") | ||
|
||
title = to_markdown(response.css("h1").get().strip()).removeprefix("# ") | ||
assert title | ||
|
||
body = response.css("div#body-content") | ||
# Drop the navigation sidebar so that we only get the main content | ||
body.css("aside").drop() | ||
|
||
markdown = to_markdown(body.get(), response.url) | ||
extractions = { | ||
"url": response.url, | ||
"markdown": markdown, | ||
} | ||
return extractions | ||
|
||
|
||
def to_markdown(html: str, base_url: Optional[str] = None) -> str: | ||
h2t = html2text.HTML2Text() | ||
|
||
# Refer to https://github.com/Alir3z4/html2text/blob/master/docs/usage.md and html2text.config | ||
# for options: | ||
# 0 for no wrapping | ||
h2t.body_width = 0 | ||
h2t.wrap_links = False | ||
|
||
if base_url: | ||
h2t.baseurl = base_url | ||
|
||
# Exclude the <sup> and <sub> tags | ||
h2t.include_sup_sub = False | ||
|
||
markdown = h2t.handle(html) | ||
|
||
# Consolidate newlines | ||
markdown = re.sub(r"\n\n+", "\n\n", markdown) | ||
return markdown.strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Help text generation can be useful in the future for usage as complexity of the CLI app increases