A beautiful soup if the class "Contains" or "Regular Expression"?

If the names of my classes are constantly different, say, for example:

listing-col-line-3-11 dpt 41 listing-col-block-1-22 dpt 41 listing-col-line-4-13 CWK 12 

Usually I could do:

 for EachPart in soup.find_all("div", {"class" : "ClassNamesHere"}): print EachPart.get_text() 

There are too many class names to work here, so there aren’t enough of them.

I know that Python does not have ".contains", which I usually used, but it has "in". Although I could not develop a way to enable this.

I hope to find a way to do this with regex. Although again my Python syntax does fail me, I am trying to change the options:

 regex = re.compile('.*listing-col-.*') for EachPart in soup.find_all(regex): 

But that doesn't sound like a trick.

+5
source share
2 answers

BeautifulSoup supports a CSS selector that allows you to select elements based on the contents of certain attributes. This includes the selector *= for contains.

Next, all div elements with the class attribute containing the text "listing-col -" will be returned:

 for EachPart in soup.select('div[class*="listing-col-"]'): print EachPart.get_text() 
+7
source

Yu can try:

 regex = re.compile('.*listing-col-.*') for EachPart in soup.find_all("div", {"class" : regex}): print EachPart.get_text() 
+1
source

Source: https://habr.com/ru/post/1240105/


All Articles