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

Automated CSS selector translation to vector form #131

Open
Immortalin opened this issue Jul 19, 2015 · 5 comments
Open

Automated CSS selector translation to vector form #131

Immortalin opened this issue Jul 19, 2015 · 5 comments
Labels

Comments

@Immortalin
Copy link

Hi, is there a way to automatically translate standard css selectors to the vector form? I am using a browser plugin to automatically generate them en mass to input into a scraper and it is really tedious to have to translate them manually. Thanks in advance!

@blx
Copy link

blx commented Jul 23, 2015

Hi - I wrote this as a learning exercise, you're welcome to poke around at it and adapt if you like: https://gist.github.com/blx/e6970121d629ca2dbd49

It's pretty rough and doesn't support > selectors or commas yet, but works for :selectors and [attributes], plus the normal classes/IDs. Of course, you could probably use an actual css parser, but what would be the fun in that..

Example:

user=> (require '[eac])
nil
user=> eac/testcss
"p#x body.blue:not(.red) #lol:first-of-type input[type=text] span:nth-child(3n + 1 )"
user=> (eac/translate-css eac/testcss)
[:p#x [:body.blue (html/but :.red)] [:#lol html/first-of-type] [:input (attr= :type "text")] [:span (html/nth-child 3 1)]]

@Immortalin
Copy link
Author

@blx is https://gist.github.com/Jared314/5028617 a complete one?

On 24 July 2015 07:01:55 GMT+08:00, Ben Cook notifications@github.com wrote:

Hi - I wrote this as a learning exercise, you're welcome to poke around
at it and adapt if you like:
https://gist.github.com/blx/e6970121d629ca2dbd49

It's pretty rough and doesn't support > selectors or commas yet, but
works for :selectors and [attributes], plus the normal classes/IDs.
Of course, you could probably use an actual css parser, but what would
be the fun in that..

Example:

user=> (require '[eac])
nil
user=> eac/testcss
"p#x body.blue:not(.red) #lol:first-of-type input[type=text]
span:nth-child(3n + 1 )"
user=> (eac/translate-css eac/testcss)
[:p#x [:body.blue (html/but :.red)] [:#lol html/first-of-type] [:input
(attr= :type "text")] [:span (html/nth-child 3 1)]]

Reply to this email directly or view it on GitHub:
#131 (comment)

@blx
Copy link

blx commented Jul 24, 2015

@Immortalin That doesn't currently seem to support pseudoclasses or attributes:

user=> (gist/parse-css "div.blue:first-of-type input.red[type=text]")
[:div.blue :input.red]

but it looks like you could add formatters for those reasonably easily if necessary!

@Immortalin
Copy link
Author

@blx can you give me a example? I am rather new to Clojure.

@Immortalin
Copy link
Author

@cgrand is it possible to expose a function/api to allow usage of css selector strings directly via Jsoup? Something like this. This would be extremely helpful as there is currently no good automated solution for translating the css selectors generator by browser plugins such as Selector Gadget, furthermore, it would be more performant as Jsoup can be utilized to parse the css selector directly.

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

No branches or pull requests

3 participants