seek in text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monkw
    New Member
    • Feb 2008
    • 2

    seek in text

    hi
    i have to find in html code some key worlds and write them to the file.
    html text looks like this:
    Code:
     ... >EUR/SEK 1:1</a></td><td><div id=aq_eursek_o>9.3055</div></td><td><div id=aq_eursek_h>9.3216</div></td><td><div id=aq_eursek_l>9.2905</div></td> ...
    and i need value that is after " id=aq_eursek_h> "

    2nd question:
    i made it using sed script, how can i run sed script in python script?
  • Subsciber123
    New Member
    • Nov 2006
    • 87

    #2
    Originally posted by monkw
    hi
    i have to find in html code some key worlds and write them to the file.
    html text looks like this:
    Code:
     ... >EUR/SEK 1:1</a></td><td><div id=aq_eursek_o>9.3055</div></td><td><div id=aq_eursek_h>9.3216</div></td><td><div id=aq_eursek_l>9.2905</div></td> ...
    and i need value that is after " id=aq_eursek_h> "

    2nd question:
    i made it using sed script, how can i run sed script in python script?
    You can do:
    [CODE=python]S_STRING_1="id= aq_eursek_h>"
    S_STRING_2="</div>"
    string=YOUR_HTM L_CODE_HERE
    data=string[string.index(S_ STRING_1)+len(S _STRING_1):]
    data=data[:data.index(S_S TRING_2)]
    [/CODE]
    I'm not quite sure if this is what you want. It assumes that the HTML file will be exactly as you described, and simply searches for that string. You could also use the re module to do this in fewer lines (and faster), as an afterthought.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      The following will get the value using re.[code=Python]
      import re
      s = '>EUR/SEK 1:1</a></td><td><div id=aq_eursek_o> 9.3055</div></td><td><div id=aq_eursek_h> 9.3216</div></td><td><div id=aq_eursek_l> 9.2905</div></td>'
      patt = re.compile(r'id =aq_eursek_h>([0-9.]+)')
      m = patt.search(s)
      if m: print m.group(1)[/code]

      >>> 9.3216
      >>>

      Comment

      Working...