-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting named scrapers into their own files
- Loading branch information
1 parent
cf5be7d
commit 110b64c
Showing
10 changed files
with
373 additions
and
325 deletions.
There are no files selected for viewing
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,23 @@ | ||
from typing import Dict, Type | ||
from .types import NamedScraper | ||
|
||
from .canonical import CanonicalScraper | ||
from .europresse import EuropresseScraper | ||
from .images import ImagesScraper | ||
from .metas import MetasScraper | ||
from .rss import RssScraper | ||
from .title import TitleScraper | ||
from .urls import UrlsScraper | ||
|
||
NAMED_SCRAPERS: Dict[str, Type[NamedScraper]] = { | ||
s.name: s | ||
for s in [ | ||
TitleScraper, | ||
CanonicalScraper, | ||
UrlsScraper, | ||
ImagesScraper, | ||
MetasScraper, | ||
RssScraper, | ||
EuropresseScraper, | ||
] | ||
} |
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,31 @@ | ||
from typing import Any, cast | ||
|
||
from bs4 import SoupStrainer, BeautifulSoup | ||
|
||
from .types import NamedScraper | ||
|
||
|
||
class CanonicalScraper(NamedScraper): | ||
name = "canonical" | ||
fieldnames = ["canonical_url"] | ||
plural = False | ||
output_type = "scalar" | ||
strainer = SoupStrainer(name="link", attrs={"rel": "canonical"}) | ||
|
||
def scrape(self, soup: BeautifulSoup, context=None) -> Any: | ||
link_elem = soup.select_one("link[rel=canonical][href]") | ||
|
||
if link_elem is None: | ||
return None | ||
|
||
url = link_elem.get("href") | ||
|
||
if url is None: | ||
return None | ||
|
||
url = cast(str, url).strip() | ||
|
||
if not url: | ||
return None | ||
|
||
return url |
Oops, something went wrong.