ó
    XŸÊh¯#  ã                   ó  • S r SSKrSSKrSSKJr  SSKJrJr  SSKJ	r	  SSK
r
SSKJr  S\S\4S	 jrS
\S\4S jrS\S\S\S\\\\\\4      4S jrS\S\\\\\\4      4S jrS\\\\\\4      S\SS4S jrSS jr\S:X  a  \" 5         gg)uí	  
Parser for IVKâ€‘group threaded adjustable supports using requests and BeautifulSoup.

This script scrapes product data from IVKâ€‘group.ru without relying on
any browser automation.  The site renders the list of products
serverâ€‘side, so a standard HTTP request is sufficient to obtain the
necessary HTML.  Each product card lives inside a ``div`` with class
``jshop-products__col``.  The article number and product URL can be
found in the child anchor under ``.list-product_name``.  The table of
prices is contained in the ``.pricetable`` element; we take the
lowest price (associated with the largest quantity, â€œÐžÑ‚Â 10000â€�)
by selecting the last numeric value followed by the currency symbol
``Ñ€``.

Algorithm:

1.  Fetch the category page and locate the category title in the
    first ``<h1>`` tag.  Convert this title from plural to singular
    feminine form using a simple heuristic: adjectives ending in
    ``Ñ‹Ðµ`` or ``Ð¸Ðµ`` become ``Ð°Ñ�``, and the final noun ending in
    ``Ñ‹`` or ``Ð¸`` is replaced by ``Ð°`` or ``Ñ�`` respectively.  This
    heuristic handles cases like â€œÐŸÐ»Ð°Ñ�Ñ‚Ð¸ÐºÐ¾Ð²Ñ‹Ðµ Ñ€ÐµÐ·ÑŒÐ±Ð¾Ð²Ñ‹Ðµ Ñ€ÐµÐ³ÑƒÐ»Ð¸Ñ€ÑƒÐµÐ¼Ñ‹Ðµ
    Ð¾Ð¿Ð¾Ñ€Ñ‹â€� â†’ â€œÐŸÐ»Ð°Ñ�Ñ‚Ð¸ÐºÐ¾Ð²Ð°Ñ� Ñ€ÐµÐ·ÑŒÐ±Ð¾Ð²Ð°Ñ� Ñ€ÐµÐ³ÑƒÐ»Ð¸Ñ€ÑƒÐµÐ¼Ð°Ñ� Ð¾Ð¿Ð¾Ñ€Ð°â€�.

2.  Paginate through the category by appending ``?start=N`` to the
    URL, where ``N`` takes values 0, 30, 60, â€¦  Each page shows up to
    30 products.  Stop when a page contains no product cards.

3.  For each page, parse the HTML with BeautifulSoup.  Iterate over
    every ``div.jshop-products__col`` to extract:

      â€¢ **Article**: text of the anchor inside ``.list-product_name``.
      â€¢ **URL**: the absolute URL from the same anchor's ``href``.
      â€¢ **Price**: from ``.pricetable``, collect all substrings
        matching the pattern ``\d+[.,]\d+`` followed by ``Ñ€`` and
        select the last one (lowest price).  Format it as ``<price>
        Ñ€ÑƒÐ±.``.
      â€¢ **Full name**: concatenate the singular category title and the
        article number.

4.  Write the collected data into a CSV file with semicolon
    separators and columns ``Ð�Ñ€Ñ‚Ð¸ÐºÑƒÐ»``, ``Ð�Ð°Ð¸Ð¼ÐµÐ½Ð¾Ð²Ð°Ð½Ð¸Ðµ``, ``URL`` and
    ``Ð¦ÐµÐ½Ð°``.

Example usage::

    python ivk_parser_bs.py --category_url https://ivk-group.ru/shop/opory-reguliruemye/rezbovye --output products.csv

Note: Internet access is required at runtime.  This script only
illustrates the parsing logic and cannot be tested in the sandbox
environment where network requests are blocked.
é    N)ÚArgumentParser)ÚListÚTuple)Úurljoin)ÚBeautifulSoupÚcategoryÚreturnc                 óª  • U R                  5       R                  5       nU(       d  U $ / n[        U5       GHŠ  u  p4UR                  5       nUnU[	        U5      S-
  :X  a–  UR                  S5      (       a4  USS S-   nUS   R                  5       (       a  UR                  5       OUnGOUR                  S5      (       a3  USS S-   nUS   R                  5       (       a  UR                  5       OUnO¼UnO¹UR                  S	5      (       d  UR                  S
5      (       a3  USS S-   nUS   R                  5       (       a  UR                  5       OUnOZUR                  S5      (       aB  [	        U5      S:”  a3  USS S-   nUS   R                  5       (       a  UR                  5       OUnOUnUR                  U5        GM�     SR                  U5      $ )uB  Convert a plural Russian category name to singular feminine.

This function implements a very simple heuristic tailored for
IVKâ€‘group category titles.  It processes the words one by one:

  * For adjectives ending with ``Ñ‹Ðµ`` or ``Ð¸Ðµ`` (e.g.
    ``ÐŸÐ»Ð°Ñ�Ñ‚Ð¸ÐºÐ¾Ð²Ñ‹Ðµ``, ``Ñ€ÐµÐ·ÑŒÐ±Ð¾Ð²Ñ‹Ðµ``, ``Ñ€ÐµÐ³ÑƒÐ»Ð¸Ñ€ÑƒÐµÐ¼Ñ‹Ðµ``) it
    substitutes the ending with ``Ð°Ñ�``.  The result preserves
    the original capitalisation of the first letter.
  * For the final noun, if it ends with ``Ñ‹`` it is replaced
    by ``Ð°``; if it ends with ``Ð¸`` it is replaced by ``Ñ�``.

These rules handle the most common patterns of plural adjectives
and nouns and are sufficient for the "ÐŸÐ»Ð°Ñ�Ñ‚Ð¸ÐºÐ¾Ð²Ñ‹Ðµ Ñ€ÐµÐ·ÑŒÐ±Ð¾Ð²Ñ‹Ðµ
Ñ€ÐµÐ³ÑƒÐ»Ð¸Ñ€ÑƒÐµÐ¼Ñ‹Ðµ Ð¾Ð¿Ð¾Ñ€Ñ‹" example.  Words that do not match the
patterns are returned unchanged.
é   u   Ñ‹Néÿÿÿÿu   Ð°r   u   Ð¸u   Ñ�u   Ñ‹Ðµu   Ð¸Ðµéþÿÿÿu   Ð°Ñ�é   Ú )
ÚstripÚsplitÚ	enumerateÚlowerÚlenÚendswithÚisupperÚ
capitalizeÚappendÚjoin)r   ÚwordsÚresultÚidxÚwordÚlwÚnew_wordÚcores           Úivk_parser_bs.pyÚto_singular_femininer"   @   s‰  € ð$ �N‰NÓ×"Ñ"Ó$€EÞØˆØ€FÜ˜u×%‰	ˆØ�Z‰Z‹\ˆØˆà”#�e“*˜q‘.Ó Ø�{‰{˜4× Ñ à˜#˜2�w ‘~�Ø04°Q±·±×0AÑ0A˜4Ÿ?™?Ô,Àt’Ø—‘˜T×"Ñ"à˜#˜2�w ‘~�Ø04°Q±·±×0AÑ0A˜4Ÿ?™?Ô,Àt‘à‘ð �{‰{˜6×"Ñ" b§k¡k°&×&9Ñ&9Ø˜#˜2�w Ñ'�Ø04°Q±·±×0AÑ0A˜4Ÿ?™?Ô,Àt‘Ø—‘˜T×"Ñ"¤s¨2£w°£{à˜#˜2�w ‘~�Ø04°Q±·±×0AÑ0A˜4Ÿ?™?Ô,Àt‘à�Ø�‰�h×ñ5 &ð6 �8‰8�FÓÐó    Ú
pricetablec                 óœ   • U c  gU R                  SSS9n[        R                  " SU5      nU(       d  gUS   R                  SS5      nU S	3$ )
uk  Extract the lowest price from a pricetable element.

The price table contains several price entries and quantity
markers.  We search for patterns like ``7.90 Ñ€.`` or ``6,70 Ñ€``.
The rightmost match corresponds to the price for the largest
quantity bracket ("ÐžÑ‚ 10000").  The returned price is formatted
as ``<value> Ñ€ÑƒÐ±.`` with a dot as decimal separator.
Ú r   T©r   u   (\d+[.,]\d+)\s*Ñ€r   Ú,Ú.u    Ñ€ÑƒÐ±.)Úget_textÚreÚfindallÚreplace)r$   ÚtextÚmatchesÚlowests       r!   Úextract_pricer1   t   s\   € ð ÑØØ×Ñ˜s¨$ÐÐ/€Dä�jŠjÐ-¨tÓ4€GÞØØ�R‰[× Ñ   cÓ*€FØˆX�XÐÐr#   ÚhtmlÚbase_urlÚsingular_categoryc                 ó|  • [        U S5      nUR                  S5      n/ nU H–  nUR                  S5      nU(       d  M  UR                  SS9nUR	                  SS5      n	[        X5      n
UR                  S5      nU(       a  [        U5      OSnU S	U 3R                  5       nUR                  X�X¬45        M˜     U$ )
zsParse a single category page and return product records.

Each record is a tuple (article, full_name, url, price).
úhtml.parserz0div.jshop-products__flex div.jshop-products__colz.list-product_name aTr'   Úhrefr&   z.pricetabler   )	r   ÚselectÚ
select_oner*   Úgetr   r1   r   r   )r2   r3   r4   ÚsoupÚproduct_cardsÚrecordsÚcardÚname_tagÚarticler7   ÚurlÚprice_tableÚpriceÚ	full_names                 r!   Ú
parse_pagerE   ˆ   s¾   € ô
 ˜˜}Ó-€DØ—K‘KÐ RÓS€MØ/1€GÛˆà—?‘?Ð#9Ó:ˆÞÙØ×#Ñ#¨$Ð#Ð/ˆØ�|‰|˜F BÓ'ˆÜ�hÓ%ˆà—o‘o mÓ4ˆÞ.9”˜kÔ*¸rˆà(Ð)¨¨7¨)Ð4×:Ñ:Ó<ˆ	Ø�‰˜¨CÐ7Ö8ñ ð €Nr#   Úcategory_urlc                 óÔ  • [         R                  " 5       nUR                  R                  SS05        / nSnSn U(       a  U  SU 3OU nUR	                  USS9nUR                  5         UR                  nUS:X  a@  [        US	5      nUR                  S
5      n	U	(       a  U	R                  SS9OSn
[        U
5      n[        XpU5      nU(       d   U$ UR                  U5        US-  nM°  )zCCrawl all pages of a category and return a list of product records.z
User-AgentzeMozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36r   r&   Tz?start=é   )Útimeoutr6   Úh1r'   )ÚrequestsÚSessionÚheadersÚupdater:   Úraise_for_statusr.   r   Úfindr*   r"   rE   Úextend)rF   ÚsessionÚresultsÚstartr4   Úpage_urlÚrespr2   r;   rJ   ÚtitleÚpage_recordss               r!   Úcrawl_categoryrY   ¡   sô   € ä×ÒÓ €Gà‡O�O×Ñàð -ð	
ôð 02€GØ€EØÐØ
Þ6;�l�^ 7¨5¨'Ñ2Àˆà�{‰{˜8¨Rˆ{Ð0ˆØ×ÑÔØ�y‰yˆà�A‹:Ü   }Ó5ˆDØ—‘˜4“ˆBÞ/1�B—K‘K d�KÑ+°rˆEÜ 4°UÓ ;Ðä! $Ð6GÓHˆÞØð €Nð 	�‰�|Ô$Ø�‰ˆñ# r#   r=   Úfilenamec                 óÔ   • [        USSSS9 n[        R                  " USS9nUR                  / SQ5        U  H  nUR                  U5        M     SSS5        g! , (       d  f       g= f)	z=Write product records to a CSV file with semicolon delimiter.Úwr&   zutf-8)ÚnewlineÚencodingÚ;)Ú	delimiter)u   Ð�Ñ€Ñ‚Ð¸ÐºÑƒÐ»u   Ð�Ð°Ð¸Ð¼ÐµÐ½Ð¾Ð²Ð°Ð½Ð¸ÐµÚURLu   Ð¦ÐµÐ½Ð°N)ÚopenÚcsvÚwriterÚwriterow)r=   rZ   Úfrd   Úrecs        r!   Ú	write_csvrh   Ä   sQ   € ä	ˆh˜ R°'Ò	:¸aÜ—’˜A¨Ñ-ˆØ�‰ÒYÔZÛˆCØ�O‰O˜CÖ ñ ÷ 
;×	:Ö	:ús   �AAÁ
A'c                  ó  • [        SS9n U R                  SSSS9  U R                  SSS	S
9  U R                  5       n[        UR                  5      n[        X!R                  5        [        S[        U5       SUR                   35        g )Nz:IVK Group category parser using requests and BeautifulSoup)Údescriptionz--category_urlTzURL of the category to parse)ÚrequiredÚhelpz--outputzivk_products.csvz/Output CSV filename (default: ivk_products.csv))Údefaultrl   zParsed z products. Saved to )	r   Úadd_argumentÚ
parse_argsrY   rF   rh   ÚoutputÚprintr   )ÚparserÚargsr=   s      r!   Úmainrt   Í   sŠ   € ÜÐ(dÑe€FØ
×ÑÐ(°4Ð>\ÐÑ]Ø
×ÑØØ"Ø>ð ñ ð
 ×ÑÓ€DÜ˜T×.Ñ.Ó/€GÜˆg—{‘{Ô#Ü	ˆG”C˜“L�>Ð!5°d·k±k°]Ð
CÕDr#   Ú__main__)r	   N)Ú__doc__rc   r+   Úargparser   Útypingr   r   Úurllib.parser   rK   Úbs4r   Ústrr"   r1   rE   rY   rh   rt   Ú__name__© r#   r!   Ú<module>r~      sî   ðñ3ój Û 	Ý #ß Ý  ã Ý ð1 3ð 1¨3ô 1ðh˜mð °ô ð(�Sð  Cð ¸Cð ÀDÈÈsÐTWÐY\Ð^aÐOaÑIbÑDcô ð2  ð  ¨¨e°C¸¸cÀ3Ð4FÑ.GÑ)Hô  ðF!�t˜E # s¨C°Ð"4Ñ5Ñ6ð !À#ð !È$ô !ôEð ˆzÓÙ…Fð r#   