***Editted: added second problem

I have this code, how do I make it stop once it's found the first instance of the if happening. break, myparser.close(), and return don't work.

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):   
        if tag == 'a' and (attrs[0][1].find('downloads&showfile') != -1):
            print(attrs[0][1])
            print(attrs[1][1])
       

info = open('download.txt', 'r').read()
myparser = MyHTMLParser()
myparser.feed(info)

2.
I have this code, How do i make it so blahblah is the name of hte file i downloaded. The name of the file is not shown in the url.

file = urllib.request.urlopen('http://beta.getdota.com/index.php?app=downloads&module=display&section=download&do=confirm_download&id=' + current)
output = open(blahblah ,'wb')
output.write(file.read())
output.close()

Edited 6 Years Ago by Enders_Game: n/a

After print(attrs[1][1]) , maybe you can put a line self.reset() ?

I am just guessing. I've never used HTMLParser before.

Can you feed HTMLParser one record at a time until the data is found? It would depend on the data type of course (i;m not an HTMLParser user either).

while not myparser.found:
    ctr += 1
    myparser.feed(info[ctr])

Edited 6 Years Ago by woooee: n/a

I think the best solution is to raise an exception in handle_starttag when the if case is encountered. Then you write

try:
    parser.feed(info)
    parser.close()
except MyCustomError:
    pass

Edited 6 Years Ago by Gribouillis: n/a

This is somewhat what Gribouillis means:

from html.parser import HTMLParser

class FoundMatchException(Exception):
   def __init__(self, value):
       self.parameter = value
   def __str__(self):
       return repr(self.parameter)


class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):   
        if tag == 'a' and (attrs[0][1].find('downloads&showfile') != -1):
            print(attrs[0][1])
            print(attrs[1][1])
            raise FoundMatchException("Stopping parser.")
       

info = open('download.txt', 'r').read()
myparser = MyHTMLParser()
try:
    myparser.feed(info)
    myparser.close()
except FoundMatchException:
    pass
finally:
    #whatever cleanup code you want.

For your 2nd problem, I know how to do it in .NET, and I'm looking for a way to do that using the Python standard library.

EDIT:
does this work?

file = urllib.request.urlopen('http://beta.getdota.com/index.php?app=downloads&module=display&section=download&do=confirm_download&id=' + current)
fname = file.info().get_filename()
output = open(fname ,'wb')
output.write(file.read())
output.close()

Edited 6 Years Ago by jcao219: n/a

Thanks a lot for the responses guys.

Here's my solution i found if you guys are curious. I havent gotten rid of the redundant code yet though. I didn't post exactly what i wanted to do with the code in my original question so sorry bout htat.

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    x = None
    def handle_starttag(self, tag, attrs):
        if tag == 'a' and (attrs[0][1].find('downloads&showfile') != -1):
            self.x = attrs[0][1]
    def get_info(self):
        return self.x

info = open('download.txt', 'r').read()
parser = MyHTMLParser()

for line in info:
    parser.feed(line)
    temp = parser.get_info()
    if temp != None:
        break
print(temp)

Also @jcao
your second solution worked perfectly thankyou very much ^^

Edited 6 Years Ago by Enders_Game: n/a

This article has been dead for over six months. Start a new discussion instead.