Can't get a regex pattern to work in Python -
i have following (repeating) html text need extract values using python , regular expressions.
<tr> <td width="35%">demand no</td> <td width="65%"><input type="text" name="t1" size="12" onfocus="this.blur()" value="876716001"></td> </tr> i can first value using
match_det = re.compile(r'<td width="35.+?">(.+?)</td>').findall(html_source_det) but above on 1 line. however, need second value on line following first 1 cannot work. have tried following, won't match
match_det = re.compile('<td width="35.+?">(.+?)</td>\n' '<td width="65.+?value="(.+?)"></td>').findall(html_source_det) perhaps unable work since text multiline, added "\n" @ end of first line, thought resolve did not.
what doing wrong?
the html_source retrieved downloading (it not static html file outlined above - put here see text). maybe not best way in getting source.
i obtaining html_source this:
new_url = "https://webaccess.site.int/curracc/" + url_details #not real url myresponse_det = urllib2.urlopen(new_url) html_source_det = myresponse_det.read()
please not try parse html regex, not regular. instead use html parsing library beautifulsoup. make life lot easier! here example beautifulsoup:
from bs4 import beautifulsoup html = '''<tr> <td width="35%">demand no</td> <td width="65%"><input type="text" name="t1" size="12" onfocus="this.blur()" value="876716001"></td> </tr>''' soup = beautifulsoup(html) print soup.find('td', attrs={'width': '65%'}).findnext('input')['value'] or more simply:
print soup.find('input', attrs={'name': 't1'})['value']
Comments
Post a Comment