Comments (7)
Also, I just wanted to say thank you for what you do.
from soupsieve.
I don't have just a simple regex to tell if an entire complex selector is good. In general, I'm basically tokenizing the selectors.
To better explain, I start at the beginning and I look for allowed things identifiers (tags), classes (identifiers preceded by .
), pseudo-class names (identifiers preceded by :
). If a pseudo class is followed by (
, and it is recognized as a pseudo class that takes functional options, then we switch context and parse according to the rules of that pseudo-class. If we see a [
, we switch context to handle an attribute.
I'm mainly giving a general overview, the exact details may be a little different, but you get the idea. There is no simple magic regex that can tell if the entire selector is valid, we just take it apart, piece by piece, making sure each piece makes sense in context of what came before, and if it doesn't, we halt by throwing an error.
from soupsieve.
That's fair. Thank you for your time!
You should check out my tool wappybird. It's built on wapppalyzer. I use it to scan very large ranges for penetration tests to search for technologies.
from soupsieve.
@brandonscholet One thing you could do is compile the selector to test it in your own validation function.
>>> import soupsieve as sv
>>> def is_valid_selector(sel):
... try:
... sv.compile(sel)
... except (sv.SelectorSyntaxError, NotImplementedError):
... return False
... return True
...
>>> is_valid_selector('> this')
False
>>> is_valid_selector('tag.class[attr=value]')
True
from soupsieve.
You are a gentlemen and a scholar. It's quick and easy to add to the guy's script. and adds no time to the larger amount of things to parse through without adding to execution time.
from soupsieve.
Yeah, we cache recently compiled patterns, so subsequent compiles should take much less time.
from soupsieve.
Just a note for anyone else checking this issue out. Soupsieve also offers its own API. It was designed this way as we were not sure when we originally implemented this if BeautifulSoup would accept us as the official CSS selector implementation.
We can build a query before using it, and use the query directly by passing in the BeautifulSoup tag.
>>> from bs4 import BeautifulSoup
>>> import soupsieve as sv
>>> soup = BeautifulSoup('<div><span>text</span></div>', 'html.parser')
>>> query = sv.compile('div > span')
>>> query.select(soup)
[<span>text</span>]
This allows you control of the selecting process. You can catch bad selectors and then choose to continue or not continue with the query without having to issue a recompile. The API also allows performing matches with match
and finding the closest parent with closest
, etc.
One of these days, I may see if BeautifulSop can add closest
and match
into their API as well, but they are always usable through our API.
from soupsieve.
Related Issues (20)
- CDATA handling in HTML changed in lxml parser with libxml2 2.9.12 HOT 21
- Interesting psuedo class to keep an eye on `:in()` HOT 8
- Rework internal structure of "relations" HOT 1
- circular dependency /bs4 HOT 15
- Attribute selectors vs \n in values HOT 5
- Change in `:has()` CSS Level 4 spec - document our difference or update? HOT 1
- hatch? HOT 5
- Using Hatch in Python 3.6 is technically not allowed HOT 7
- setup.py is mentioned in readme but there is no setup.py HOT 2
- Invalid syntax error on python3.4 HOT 5
- Tracking `:scope` issue related to relative selector lists (`:has()`) HOT 1
- pyproject.toml: validation error since setuptools 61.2.0 HOT 8
- PermissionError: [Errno 13] Permission denied HOT 4
- missing dependency on `bs4` HOT 7
- LXML does not currently generate wheels for Python 3.11 on Windows
- `:has()` is no longer forgiving HOT 1
- The new type hints cause pytest to hang after test session HOT 4
- Attribute Selector Case Sensitivity: Whitespace HOT 1
- Potentially rework CSS parsing
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from soupsieve.