Skip to content
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
4 changes: 2 additions & 2 deletions src/pyscrappy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@

def scrape(
url: str,
selectors: dict[str, str] | None = None,
selectors: "dict[str, str] | None" = None,
max_pages: int = 1,
render_js: bool = False,
config: ScraperConfig | None = None,
config: "ScraperConfig | None" = None,
) -> ScrapeResult:
"""One-liner convenience function to scrape any URL.

Expand Down
15 changes: 10 additions & 5 deletions src/pyscrappy/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
from typing import Literal

_DEFAULT_USER_AGENTS = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0)"
" Gecko/20100101 Firefox/125.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15"
" (KHTML, like Gecko) Version/17.4 Safari/605.1.15",
]


Expand Down
4 changes: 3 additions & 1 deletion src/pyscrappy/core/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def get(self, url: str, **kwargs: Any) -> httpx.Response:
time.sleep(delay)
continue

raise NetworkError(f"Failed to fetch {url} after {self.config.max_retries} attempts") from last_exc
raise NetworkError(
f"Failed to fetch {url} after {self.config.max_retries} attempts"
) from last_exc

def get_raw(self, url: str, **kwargs: Any) -> httpx.Response:
"""Like :meth:`get` but does NOT raise on non-2xx status codes.
Expand Down
1 change: 0 additions & 1 deletion src/pyscrappy/generic/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def extract(self, soup: BeautifulSoup) -> dict[str, Any]:
}

def _remove_noise(self, soup: BeautifulSoup) -> BeautifulSoup:
from copy import copy

clean = BeautifulSoup(str(soup), "lxml")
for tag in clean.find_all(self._NOISE_TAGS):
Expand Down
1 change: 0 additions & 1 deletion src/pyscrappy/generic/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from bs4 import BeautifulSoup, Tag


_NEXT_PATTERNS = re.compile(
r"(^next$|^>$|^>>$|^›$|^»$|next\s*page|next\s*›|load\s*more)",
re.IGNORECASE,
Expand Down
1 change: 0 additions & 1 deletion src/pyscrappy/scrapers/alibaba.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from bs4 import Tag

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down
1 change: 0 additions & 1 deletion src/pyscrappy/scrapers/flipkart.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from bs4 import Tag

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down
7 changes: 2 additions & 5 deletions src/pyscrappy/scrapers/image_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

import logging
import os
import re
from typing import Any
from urllib.parse import quote_plus, urljoin

from bs4 import Tag
from urllib.parse import quote_plus

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult
from pyscrappy.core.models import ScrapeMetadata, ScrapeResult

logger = logging.getLogger("pyscrappy.image_search")

Expand Down
6 changes: 4 additions & 2 deletions src/pyscrappy/scrapers/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from typing import Any

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down Expand Up @@ -218,7 +217,10 @@ def _parse_hashtag_json(
node = edge.get("node", {})
caption_edges = node.get("edge_media_to_caption", {}).get("edges", [])
posts.append({
"caption": caption_edges[0].get("node", {}).get("text", "") if caption_edges else "",
"caption": (
caption_edges[0].get("node", {}).get("text", "")
if caption_edges else ""
),
"likes": node.get("edge_liked_by", {}).get("count"),
"comments": node.get("edge_media_to_comment", {}).get("count"),
"url": f"https://www.instagram.com/p/{node.get('shortcode', '')}/",
Expand Down
1 change: 0 additions & 1 deletion src/pyscrappy/scrapers/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ def _parse_feed_xml(self, xml_text: str, max_articles: int) -> list[dict[str, An
return articles

# Namespace handling for Atom
ns = {"atom": "http://www.w3.org/2005/Atom"}

# RSS 2.0: <channel><item>
for item in root.iter("item"):
Expand Down
1 change: 0 additions & 1 deletion src/pyscrappy/scrapers/soundcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from urllib.parse import quote_plus

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down
1 change: 0 additions & 1 deletion src/pyscrappy/scrapers/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from urllib.parse import quote_plus

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down
6 changes: 4 additions & 2 deletions src/pyscrappy/scrapers/swiggy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from bs4 import Tag

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down Expand Up @@ -68,7 +67,10 @@ def scrape( # type: ignore[override]
if not restaurants:
errors.append(ScrapeError(
url=url,
message="No restaurants extracted. Swiggy requires JS rendering — use render_js=True.",
message=(
"No restaurants extracted. Swiggy requires JS rendering"
" — use render_js=True."
),
))

return ScrapeResult(
Expand Down
7 changes: 5 additions & 2 deletions src/pyscrappy/scrapers/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from typing import Any

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down Expand Up @@ -83,7 +82,11 @@ def _scrape_search(
if not tweets:
errors.append(ScrapeError(
url=url,
message="No tweets extracted. Twitter/X likely requires authentication. Use render_js=True with a logged-in browser session.",
message=(
"No tweets extracted. Twitter/X likely requires"
" authentication. Use render_js=True with a"
" logged-in browser session."
),
))

return ScrapeResult(
Expand Down
2 changes: 1 addition & 1 deletion src/pyscrappy/scrapers/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any
from urllib.parse import quote

from bs4 import BeautifulSoup, Tag
from bs4 import Tag

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
Expand Down
3 changes: 1 addition & 2 deletions src/pyscrappy/scrapers/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from urllib.parse import quote_plus

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult
from pyscrappy.core.models import ScrapeMetadata, ScrapeResult


class YouTubeScraper(BaseScraper):
Expand Down
11 changes: 8 additions & 3 deletions src/pyscrappy/scrapers/zomato.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from bs4 import Tag

from pyscrappy.core.base import BaseScraper
from pyscrappy.core.config import ScraperConfig
from pyscrappy.core.models import ScrapeError, ScrapeMetadata, ScrapeResult


Expand Down Expand Up @@ -72,7 +71,10 @@ def scrape( # type: ignore[override]
if not restaurants:
errors.append(ScrapeError(
url=url,
message="No restaurants extracted. Zomato requires JS rendering — use render_js=True.",
message=(
"No restaurants extracted. Zomato requires JS rendering"
" — use render_js=True."
),
))

return ScrapeResult(
Expand Down Expand Up @@ -198,7 +200,10 @@ def _find_restaurants(
"rating": info.get("rating", {}).get("aggregate_rating")
if isinstance(info.get("rating"), dict)
else info.get("rating"),
"price": info.get("average_cost_for_two") or info.get("cfo", {}).get("text", ""),
"price": (
info.get("average_cost_for_two")
or info.get("cfo", {}).get("text", "")
),
"address": info.get("location", {}).get("address", "")
if isinstance(info.get("location"), dict) else "",
"url": f"https://www.zomato.com/restaurant/{info.get('id', '')}",
Expand Down
Loading