-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from zytedata/normalize-processor
Normalizer processor
- Loading branch information
Showing
5 changed files
with
85 additions
and
8 deletions.
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 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 @@ | ||
import re | ||
|
||
from scrapy.utils.url import parse_url | ||
|
||
from .base import UrlProcessorBase | ||
|
||
|
||
class NormalizerProcessor(UrlProcessorBase): | ||
def validate_args(self) -> None: | ||
if self.args: | ||
raise TypeError(f"normalizeUrl doesn't accept args, got: {self.args}") | ||
|
||
def process(self, input_url: str) -> str: | ||
"""Normalizes the input URL by removing the following: | ||
* 'www.' prefixes including ones with numerical characters, e.g. 'www2.' | ||
* trailing slashes | ||
""" | ||
|
||
parsed_url = parse_url(input_url) | ||
netloc = re.sub(r"^www\d*\.", "", parsed_url.netloc) | ||
path = parsed_url.path.rstrip("/") | ||
return parsed_url._replace(netloc=netloc, path=path).geturl() |
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