954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search particular text in HTML using beautiful soup and python

Hi all,

I just started learning Python and Beautiful Soup. I am developing a script to look for particular text "Running" in the HTML. If that text "Running" exits, I would like to print out the keyword "QSAJK". Please see the attached image to visualize. If there is one or more text of "Running", the script should also print out respective keywords. Below is my code to open the webpage and search for the text. How should I modify to fulfill my requirements. Thank you very much in advance.

import time
import urllib2
import re
from BeautifulSoup import BeautifulSoup
from BeautifulSoup import NavigableString

def Check_Tester():

  soup = BeautifulSoup(urllib2.urlopen("http://compat.sing.seagate.com/servlets/ReportMgrSecure?REPORT=rebootManager&RACK=SG-HFT-15&CLOSE=1&ORDERBY=sporder").read())
  Key_Word = soup.findAll('td',text='Running')
  
def main():
  
  Check_Tester()
  
main()
Attachments Reset.jpg 54.94KB
Aung Myat
Newbie Poster
2 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

I'm not familiar with Beautiful Soup but I assume that soup (in your code) is a text string. If I'm correct then you want to be using string replace methods:

str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

If you have to make sure that your 'Running' is not in the middle of a word (outrunning), or it case is unimportant, or if there are additional considerations then you may have to move up to the re (regular expression) module

re.sub - http://docs.python.org/py3k/library/re.html?highlight=re.#re.sub

tomstratton
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

Yes, that's true Beautifullsoup can take regular expressions. I have not used it myself but there seems to be quite a lot documentation and examples around. Where you learned to use it? Maybe there is something to learn still, from for example http://www.crummy.com/software/BeautifulSoup/download/2.x/documentation.html

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I have been using regular expressions for so long in so many languages I can't remember where I learned it. The python docs are the place to start...

tomstratton
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You