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

Allow regular expressions in text_contains / any_text_contains #92

Open
kelvindecosta opened this issue Jun 12, 2023 · 2 comments
Open

Comments

@kelvindecosta
Copy link

I would like to find all script tags within a page with contain certain keywords.

Here's how it's done via bs4:

script_tags = soup.find_all("script", string=re.compile(r"window\.(keyword|another_keyword)"))

I'm not too sure how to go about it with selectolax.

I could use the matches provided by .select(css).text_contains(pattern) like so:

first_keyword_scripts = tree.select("script").text_contains("window.keyword").matches

However, if I were to do the same for the second keyword, it is difficult to create the original context created with bs4 in which:

  • script_tags preserved the order of the script tags. In this case, I have to figure out which nodes come first, and need to merge them somehow
  • some scripts might contain both keywords and as such can result in duplication

Another use case for regular expressions is the ability to ignore keywords.
It would be nice to have a text_does_not_contain function too.

I think these issues can be solved with regular expressions but I'm probably wrong.

I'd appreciate any feedback on how these examples can be run via selectolax.
For now I have a workaround that uses the .text() operation, which isn't really great for large script texts,

Thanks for your time and for maintaining this project!

@rushter
Copy link
Owner

rushter commented Jun 13, 2023

I think I can implement a callback interface so that you can provide a custom function that executes the regex internally.

@kelvindecosta
Copy link
Author

@rushter , thank you so much for considering this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants