Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:51:16+00:00 2026-05-26T09:51:16+00:00

I’m am writing a bit of python code that gets data from a website.

  • 0

I’m am writing a bit of python code that gets data from a website. The table is well formed and everything is working fine most of the time.

However, when the parser encounters a blank field, it totally ignores it. I need it to count the blank space but I can’t figure out how to do this.

The problem lies with some arrays I am using that are giving me out of bounds errors.

Anyway, here’s my code:

class MyParser(HTMLParser):
    def __init__(self, *args, **kwargs):
        #There are only 2 tables in the source code. Outer one is useless to me
        self.outerloop = True
        #Set to true when we are in the table, and we want to collect data
        self.capture_data = False
        #Array to store the captured data
        self.dataArray = []
        HTMLParser.__init__(self, *args, **kwargs)

    def handle_starttag(self, tag, attrs):
        if tag == 'table' and self.outerloop:
            self.outerloop=False
        elif tag=='td' and not self.outerloop:
            self.capture_data=True
        elif tag=='th':
            self.capture_data=False

    def handle_endtag(self, tag):
        if tag == 'table':
            self.capture_data=False

    def handle_data(self, data):
        if self.capture_data:
            self.dataArray.append(data)

#Function to call the parser
def getData(self):
    self.p = MyParser()

    url = 'http://www.mysite.com/get.php'
    content = urllib.urlopen(url).read()
    self.p.feed(content)

    val=0
    resultString=""

    while val < len(self.p.dataArray):
        resultString+=self.p.dataArray[val]+","
        val+=1

    return HttpResponse(resultString[:-1])

The problem lies in the handle_data function. Somehow in there I need to tell it to store <td></td> as , eg a blank string. This is important since I output the string to my webpage as a comma seperated list of values, as can be seen at the bottom.

I’d be very grateful for anyone who can help me with this.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:51:17+00:00Added an answer on May 26, 2026 at 9:51 am

    OK, I know its frowned upon for answering your own question, but in case anyone comes across this problem in the future, I’ll just put up my source.

    I fixed it by having 2 integers. They both start off at 0. When I encountered the opening tag in questin, I would increment ONE of the numbers. When the data was handled, I then incremented the 2nd number. When I encountered the closing tag for this particular tag, I checked to see if the numbers were equal, which they should be if the data was consumed.

    If it turned out the numbers were not equal, then it would mean that the program did not handle the data, ie a blank tag. I then simply appended N/A to the array and got it working.
    See here:

    class MyHTMLParser(HTMLParser):
        def __init__(self, *args, **kwargs):
            self.outerloop = True
            self.capture_data = False
            self.dataArray = []
            self.celldata="NA"
            self.firstnum=0
            self.secondnum=0
            HTMLParser.__init__(self, *args, **kwargs)
    
        def handle_starttag(self, tag, attrs):
            if tag == 'table' and self.outerloop:
                self.outerloop=False
            elif tag=='td' and not self.outerloop:
                self.capture_data=True # bool to indicate we want to capture data
                self.firstnum+=1    # increment first num to say we have encountered the tag in question
            elif tag=='th':
                self.capture_data=False
    
        def handle_endtag(self, tag):
            if tag == 'table':
                self.capture_data=False
            elif tag == 'td' and not self.firstnum == self.secondnum:   #check if they are not equal
                self.dataArray.append(self.celldata)    # append filler data
                self.secondnum=self.firstnum    # make them equal for next tag
    
        def handle_data(self, data):
            if self.capture_data::
                self.dataArray.append(data)
                self.secondnum=self.firstnum
    
    def getTides(self):
        self.p = MyHTMLParser()
    
        url = 'http://www.mysite.com/page.php'
        content = urllib.urlopen(url).read()
        self.p.feed(content)
    
        val=0
        resultString=""
    
        while val < len(self.p.dataArray):
            resultString+=self.p.dataArray[val]+","
            val+=1
    
        return HttpResponse(resultString[:-1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.