How do I extract tags with ::marker from HTML using beautiful soup

Viewed 252

I am trying to find li elements that have ::marker such as seen below using BeautifulSoup.

I tried using cssutils but was unsuccessful (maybe I am using it wrong)

enter image description here

Pseudo code:

lis = soup_obj.find_all("li")
for li in lis:
  if li (has :: marker): # This is what I would like to do 
     print(li)

Thank you in advance for your help.

2 Answers

AFAIK you won't be able to use cssutils as the pseudo element ::marker is not supported syntax. According to https://pythonhosted.org/cssutils/README.html#overview under

Selectors

The selector syntax defined here (and not in CSS 2.1) should be parsable with cssutils (should mind though ;) )

You see supported should be (in section 7):

7.1.The ::first-line pseudo-element
7.2.The ::first-letter pseudo-element
7.4.The ::before and ::after pseudo-elements

Additionally, no pseudo elements are supported by Soup Sieve:

Soup Sieve aims to allow users to target XML/HTML elements with CSS selectors. It implements many pseudo classes, but it does not currently implement any pseudo elements and has no plans to do so.

Perhaps relevant is whether you can try using JavaScript via Selenium automation. Discussions of note are comments here:

How to select and manipulate specific <li> elements present with pseudo-elements such as ::marker, ::before using javascript. Though that discussion is about getComputedStyle which is different from your end goal.


An example page, if others want to have a play, are the bottom bullets in this page:

https://webdesign.tutsplus.com/tutorials/next-level-list-bullets-with-css-marker--cms-37212

For as long as that link remains live.


It could be there is another pattern you could use to target the desired nodes. It would help therefore, to share the link if public and freely accesible.

The simplest work-around that I have found was to use:

list(li.stripped_strings)[-1]
Related