code_junkie

Python regular expression for HTML parsing (BeautifulSoup)

Nov 10th, 2011
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. <input type="hidden" name="fooId" value="12-3456789-1111111111" />
  2.  
  3. <input type="hidden" name="fooId" value="**[id is here]**" />
  4.  
  5. from BeautifulSoup import BeautifulSoup
  6.  
  7. #Or retrieve it from the web, etc.
  8. html_data = open('/yourwebsite/page.html','r').read()
  9.  
  10. #Create the soup object from the HTML data
  11. soup = BeautifulSoup(html_data)
  12. fooId = soup.find('input',name='fooId',type='hidden') #Find the proper tag
  13. value = fooId.attrs[2][1] #The value of the third attribute of the desired tag
  14. #or index it directly via fooId['value']
  15.  
  16. from BeautifulSoup import BeautifulSoup
  17. #Or retrieve it from the web, etc.
  18. html_data = open('/yourwebsite/page.html','r').read()
  19. #Create the soup object from the HTML data
  20. soup = BeautifulSoup(html_data)
  21. fooId = soup.find('input',name='fooId',type='hidden') #Find the proper tag
  22. value = fooId['value'] #The value attribute
  23.  
  24. import re
  25. reg = re.compile('<input type="hidden" name="([^"]*)" value="<id>" />')
  26. value = reg.search(inputHTML).group(1)
  27. print 'Value is', value
  28.  
  29. html = """<html><body>
  30. <input type="hidden" name="fooId" value="**[id is here]**" />
  31. <blah>
  32. <input name="fooId" type="hidden" value="**[id is here too]**" />
  33. <input NAME="fooId" type="hidden" value="**[id is HERE too]**" />
  34. <INPUT NAME="fooId" type="hidden" value="**[and id is even here TOO]**" />
  35. <!--
  36. <input type="hidden" name="fooId" value="**[don't report this id]**" />
  37. -->
  38. <foo>
  39. </body></html>"""
  40.  
  41. from pyparsing import makeHTMLTags, withAttribute, htmlComment
  42.  
  43. # use makeHTMLTags to create tag expression - makeHTMLTags returns expressions for
  44. # opening and closing tags, we're only interested in the opening tag
  45. inputTag = makeHTMLTags("input")[0]
  46.  
  47. # only want input tags with special attributes
  48. inputTag.setParseAction(withAttribute(type="hidden", name="fooId"))
  49.  
  50. # don't report tags that are commented out
  51. inputTag.ignore(htmlComment)
  52.  
  53. # use searchString to skip through the input
  54. foundTags = inputTag.searchString(html)
  55.  
  56. # dump out first result to show all returned tags and attributes
  57. print foundTags[0].dump()
  58. print
  59.  
  60. # print out the value attribute for all matched tags
  61. for inpTag in foundTags:
  62. print inpTag.value
  63.  
  64. ['input', ['type', 'hidden'], ['name', 'fooId'], ['value', '**[id is here]**'], True]
  65. - empty: True
  66. - name: fooId
  67. - startInput: ['input', ['type', 'hidden'], ['name', 'fooId'], ['value', '**[id is here]**'], True]
  68. - empty: True
  69. - name: fooId
  70. - type: hidden
  71. - value: **[id is here]**
  72. - type: hidden
  73. - value: **[id is here]**
  74.  
  75. **[id is here]**
  76. **[id is here too]**
  77. **[id is HERE too]**
  78. **[and id is even here TOO]**
  79.  
  80. /<input type="hidden" name="fooId" value="([d-]+)" />/
  81.  
  82. /<inputs+type="hidden"s+name="([A-Za-z0-9_]+)"s+value="([A-Za-z0-9_-]*)"s*/>/
  83.  
  84. >>> import re
  85. >>> s = '<input type="hidden" name="fooId" value="12-3456789-1111111111" />'
  86. >>> re.match('<inputs+type="hidden"s+name="([A-Za-z0-9_]+)"s+value="([A-Za-z0-9_-]*)"s*/>', s).groups()
  87. ('fooId', '12-3456789-1111111111')
Add Comment
Please, Sign In to add comment