Skip to content

Best practice using selectolax together with if and dict #65

Closed

Description

@BarryThrill

Hello there Selectolax!

I been using selectolax for a while and also hoping that you are having it well.

I came here not with an issue but rather than a best practice in my situation and I wonder how I could improve my knowledge regarding selectolax. I have written an example code of where I do a requests to a webpage -> Check if the response is 200 and whenever that is, I do another check to see if that value has already been stored -> if not then I would like to get some data from the webpage and in the future I will return the dict with stored parse.

from selectolax.lexbor import LexborHTMLParser
import requests

stored_id = {}

response = requests.get(
    'https://barrythrill.com/testing/',
    timeout=12,
)

if response.status_code == 200:
    parser = LexborHTMLParser(response.text)

    for product in parser.css('div.product-row a.product-card'):
        if product.css_first(....).get.attributes('href'....) not in self.stored_id:
                print({
                    'url': product.css_first(....).text().strip(),
                    'title': product.css_first(....).text().strip(),
                    'price': product.css_first(....).get.attributes(...),
                    'image': product.css_first(....).get.attributes(...),
                })

This is what I have written and my concern is that maybe it is not a good idea to do perhaps a if statement to see whenever its there or not and also if its good to do that way when scraping the data and inserting it to a dict? My main goal is to have the fastest speed to get the data

Please feel free to close this ticket if you feel like its abit too off-topic :D

Activity

  1. rushter commented on Aug 19, 2022

    @rushter
    Owner

    I think using .attrs instead of .attributes can be a little bit faster since it does not retrieve all the attributes. But the difference should be very small.

    So, try .attrs.get('href') instead of .attributes().get('href').

  2. BarryThrill commented on Aug 19, 2022

    @BarryThrill
    ContributorAuthor

    I think using .attrs instead of .attributes can be a little bit faster since it does not retrieve all the attributes. But the difference should be very small.

    So, try .attrs.get('href') instead of .attributes().get('href').

    Appreciate that! I believe there isn't much other stuff to improve? What Im thinking maybe what could happend if the "text()" is not found - I believe that throws an exception but then that shouldn't happen whenever there is response 200 either way.

    Thank you so much! You can close if you feel that there isn't other things to improve!

  3. Repository owner locked and limited conversation to collaborators on Aug 23, 2022
  4. converted this issue into a discussion #66 on Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions