beautifulsoup - Beautiful Soup - issue with brackets in strings? -


beautiful soup seems have problem strings contain brackets, following code fail if ethernet lan (rj-45) entered keyword succeed if enter ethernet lan

does know of reason why is, there special can add code make work brackets need perfect match?

code

pattern = re.compile(r'\s*%s\s*' % 'ethernet lan (rj-45) ports quantity') rj45_ports = soup.find(text=pattern).findnext('div',{'class':'ds_data'}).text print rj45_ports 

html

<div class="tablerow"> <div class="ds_label"> <span class=""> ethernet lan (rj-45) ports quantity</span> <span class="red line"> </div> <div class="ds_data"> 1 </div> </div> 

in regex ()s special characters. need escape them.

pattern = re.compile(r'\s*%s\s*' % 'ethernet lan \(rj-45\) ports quantity') 

it may worthwhile write function handles sanitizing strings.

def sanitize(s):     out = s     # fill whatever additional meta characters need escape     meta_char in ['(', ')']:         out = out.replace(meta_char, '\\'+meta_char)     return out 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -