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

Add Bazos.cz scraper #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ DISCORD_TOKEN=CREATE_ENV_LOCAL_AND_SET_TOKEN
DISCORD_OFFERS_CHANNEL=1067564052022300672
DISCORD_DEV_CHANNEL=954732715377311825
DISPOSITIONS=3+kk,3+1

# In case you want to use Bazos.cz portal, fill in at least BAZOS_SEARCHSTRING
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tady se píše, že se musí vyplnit alespoň jeden bazoš parametr aby člověk dostával nabídky i z bazoše, ale v kódu nikde nevidím žádnou podmínku na vyplněnost těchto parametrů. Když není vyplněný žádný parametr tak i tak se webscraper snaží posílat požadavky na bazoš.

Navrhoval bych udělat metodu build_response scraperů nullable a následně v BazosScraperu vrátit null, pokud není zadán žádný bazoš parametr (a samozřejmě v get_latest_offers vrátit prázdné pole, pokud build_response vrátí null).

Toto mě také přivádí k myšlence, že by bylo dobré mít konrolovatelné které scrapery se budou používa (podobně jako nastavení dispozic). Ale na to založím issue, to není potřeba řešit v tomto PR.

BAZOS_SEARCHSTRING=
BAZOS_LOCATION=
BAZOS_RADIUS=
BAZOS_PRICE_FROM=
BAZOS_PRICE_TO=
Comment on lines +12 to +15
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tohle se mi líbí 👍

Bylo by fajn toto rozšířit na všechny scrapery, ale to je spíš poznámka pro mě a udělám na to zvlášť issue. Není potřeba řešit v tomto PR.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ remembered_offers.txt
found_offers.txt
data/
.env.local
venv/
6 changes: 6 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class Config:
refresh_interval_nighttime_minutes: int = environ.var(converter=int)
dispositions: Disposition = environ.var(converter=dispositions_converter)

bazos_searchstring: str = environ.var()
bazos_location: str = environ.var()
bazos_radius: str = environ.var()
bazos_price_from: str = environ.var()
bazos_price_to: str = environ.var()

@environ.config()
class Discord:
token = environ.var()
Expand Down
53 changes: 53 additions & 0 deletions src/scrapers/scraper_bazos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import logging

import requests
from bs4 import BeautifulSoup

from config import config
from scrapers.rental_offer import RentalOffer
from scrapers.scraper_base import ScraperBase
import requests
from bs4 import BeautifulSoup


class ScraperBazos(ScraperBase):

name = "BAZOS"
logo_url = "https://play-lh.googleusercontent.com/EPQt7rfipj_vji4uIkVo43g7OJLNc-NH6FpT_HuiJkgHbyi_-Biossm0SnOd1UQfrdw=w240-h480-rw"
color = 0xFFA500


def build_response(self) -> requests.Response:
format_str = "https://www.bazos.cz/search.php?hledat={}&hlokalita={}&humkreis={}&cenaod={}&cenado={}"
url = format_str.format(
config.bazos_searchstring,
config.bazos_location,
config.bazos_radius,
config.bazos_price_from,
config.bazos_price_to
)
logging.debug("BAZOS request: %s", url)

return requests.get(url, headers=self.headers)

def get_latest_offers(self) -> list[RentalOffer]:
response = self.build_response()
soup = BeautifulSoup(response.text, 'html.parser')

items: list[RentalOffer] = []

for item in soup.select(".inzeraty"):
image = item.find("img", "obrazek")
price = item.find("div", "inzeratycena")
about = item.find("div", "popis")

items.append(RentalOffer(
scraper=self,
link=item.find("h2", "nadpis").a.get("href"),
title=item.find("h2", "nadpis").a.get_text() or "Chybí titulek",
location=about.getText() or "Chybí popis",
price=price.b.get_text() or "0",
image_url=image.get("src")
))

return items
2 changes: 2 additions & 0 deletions src/scrapers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from scrapers.scraper_sreality import ScraperSreality
from scrapers.scraper_ulov_domov import ScraperUlovDomov
from scrapers.scraper_bezrealitky import ScraperBezrealitky
from scrapers.scraper_bazos import ScraperBazos


def create_scrapers(dispositions: Disposition) -> list[ScraperBase]:
Expand All @@ -27,6 +28,7 @@ def create_scrapers(dispositions: Disposition) -> list[ScraperBase]:
ScraperSreality(dispositions),
ScraperUlovDomov(dispositions),
ScraperBezrealitky(dispositions),
ScraperBazos(dispositions),
]


Expand Down