Reputation: 605
First time working with the HTMLParser module. Trying to use standard string formatting on the ouput, but it's giving me an error. The following code:
import urllib2
from HTMLParser import HTMLParser
class LinksParser(HTMLParser):
def __init__(self, url):
HTMLParser.__init__(self)
req = urllib2.urlopen(url)
self.feed(req.read())
def handle_starttag(self, tag, attrs):
if tag != 'a': return
for name, value in attrs:
print("Found Link --> {]".format(value))
if __name__ == "__main__":
LinksParser("http://www.facebook.com"
Produces the following error:
File "C:\Users\workspace\test\src\test.py", line 15, in handle_starttag
print("Found Link --> {]".format(value))
ValueError: unmatched '{' in format
Upvotes: 0
Views: 993
Reputation: 11929
print("Found Link --> {]".format(value))
Should instead be:
print("Found Link --> {}".format(value))
You used a square bracket instead of a brace.
Upvotes: 2
Reputation: 18695
There are several problems
handle_starttag
should be indentedhandle_starttag
you should use {0}
instead of {]
Upvotes: 0
Reputation: 74795
This format string looks broken: print("Found Link --> {]".format(value))
. You need to change this to print("Found Link --> {key}".format(key = value))
.
Upvotes: 0