Skip to content

Adding a strip kwarg to get() and getall() #249

Open
@bblanchon

Description

@bblanchon

Hi,

Thank you very much for this excellent library ❤️

I've been using Parsel for a while and I constantly find myself calling .strip() after .get() or .getall().
I think it would be very helpful if Parsel provided a built-in mechanism for that.

I suggest adding a strip kwarg to get() and getall().
It would be a boolean value, and when it's true, Parsel would call strip() on every match.

Example with get():

# Before
author = selector.css("[itemprop=author] [itemprop=name]::text").get()
if author:
   author = author.strip()

# After
author = selector.css("[itemprop=author] [itemprop=name]::text").get(strip=True)

Example with getall():

# Before
authors = [author.strip() for author in selector.css("[itemprop=author] [itemprop=name]::text").getall()]

# After
authors = selector.css("[itemprop=author] [itemprop=name]::text").getall(strip=True)

Alternatively, we could change the ::text pseudo-element to support an argument, like ::text(strip=1).
That would be extremely handy too and probably more flexible than my original suggestion, but also more difficult to implement.

I know I could strip whitespaces with re() and re_first() but it's overkill and hides the intent.

Best regards,
Benoit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions