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

remove 'www*.' prefix when parsing allowed_domains #27

Merged
merged 8 commits into from
Jan 30, 2024
Merged
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
10 changes: 10 additions & 0 deletions tests/test_ecommerce.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)

from . import get_crawler
from .test_utils import URL_TO_DOMAIN


def test_parameters():
Expand Down Expand Up @@ -618,3 +619,12 @@ def test_get_parse_navigation_request():
"page_type": "productNavigation",
},
}


@pytest.mark.parametrize("url,allowed_domain", URL_TO_DOMAIN)
def test_set_allowed_domains(url, allowed_domain):
crawler = get_crawler()

kwargs = {"url": url}
spider = EcommerceSpider.from_crawler(crawler, **kwargs)
assert spider.allowed_domains == [allowed_domain]
20 changes: 20 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from zyte_spider_templates.utils import get_domain

URL_TO_DOMAIN = (
("https://example.com", "example.com"),
("https://www.example.com", "example.com"),
("https://www2.example.com", "example.com"),
BurnzZ marked this conversation as resolved.
Show resolved Hide resolved
("https://prefixwww.example.com", "prefixwww.example.com"),
("https://wwworld.example.com", "wwworld.example.com"),
("https://my.wwworld-example.com", "my.wwworld-example.com"),
("https://wwwow.com", "wwwow.com"),
("https://wowww.com", "wowww.com"),
("https://awww.com", "awww.com"),
)


@pytest.mark.parametrize("url,domain", URL_TO_DOMAIN)
def test_get_domain(url, domain):
assert get_domain(url) == domain
2 changes: 0 additions & 2 deletions zyte_spider_templates/spiders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import scrapy
from pydantic import BaseModel, Field
from scrapy.crawler import Crawler
from scrapy.utils.url import parse_url

from zyte_spider_templates._geolocations import (
GEOLOCATION_OPTIONS_WITH_CODE,
Expand Down Expand Up @@ -67,7 +66,6 @@ class BaseSpider(scrapy.Spider):
@classmethod
def from_crawler(cls, crawler: Crawler, *args, **kwargs) -> scrapy.Spider:
spider = super().from_crawler(crawler, *args, **kwargs)
spider.allowed_domains = [parse_url(spider.args.url).netloc]

if spider.args.geolocation:
# We set the geolocation in ZYTE_API_PROVIDER_PARAMS for injected
Expand Down
2 changes: 2 additions & 0 deletions zyte_spider_templates/spiders/ecommerce.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BaseSpider,
BaseSpiderParams,
)
from zyte_spider_templates.utils import get_domain


@document_enum
Expand Down Expand Up @@ -124,6 +125,7 @@ class EcommerceSpider(Args[EcommerceSpiderParams], BaseSpider):
@classmethod
def from_crawler(cls, crawler: Crawler, *args, **kwargs) -> scrapy.Spider:
spider = super(EcommerceSpider, cls).from_crawler(crawler, *args, **kwargs)
spider.allowed_domains = [get_domain(spider.args.url)]

if spider.args.extract_from is not None:
spider.settings.set(
Expand Down
7 changes: 7 additions & 0 deletions zyte_spider_templates/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import re

from scrapy.utils.url import parse_url


def get_domain(url: str) -> str:
return re.sub(r"^www\d*\.", "", parse_url(url).netloc)
Loading