python - Beautiful Soup if Class "Contains" or Regex? -


if class names different example:

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

normally do:

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

there way many class names work here bunch of these out.

i know python doesn't have ".contains" use have "in". though haven't been able work out way incorporate that.

i'm hoping there's way regex. though again python syntax letting me down i've been trying variations on:

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

but doesn't seem doing trick.

beautifulsoup supports css selectors allow select elements based on content of particular attributes. includes selector *= contains.

the following return div elements class attribute containing text 'listing-col-':

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

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -