By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

How to exctract title of links

P: n/a
here i have a link <a href="http://raver2046.ath.cx/CV/">cv network
admin</a>

how to extract "cv network admin"
here is the code i have find to exctract link but not title of link

----------------------------
import htmllib, formatter, urllib
class x(htmllib.HTMLParser):
def dump(self, tag, attrs):
#print tag,
for a, v in attrs:
if a in ['a', 'src', 'href']:
print v,

print
#def do_img(self, attrs):
# self.dump('img', attrs)
def start_a(self, attrs):
self.dump('a', attrs)
#def start_form(self, attrs):
# self.dump('form', attrs)

y = x(formatter.NullFormatter())
y.feed(urllib.urlopen('http://www.aquabase.org/fish/dump.php3').read())
y.close()
----------------------------

http://raver2046.ath.cx/CV/cv_fr.html
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
import htmllib, formatter, urllib
class x(htmllib.HTMLParser):
inanchor = True # indicates whether we are inside anchor element
def dump(self, tag, attrs):
#print tag,
for a, v in attrs:
if a in ['a', 'src', 'href']:
print v,
print
#def do_img(self, attrs):
# self.dump('img', attrs)
def start_a(self, attrs):
self.dump('a', attrs)
self.inanchor = True # yes now we are in anchor element
def handle_data(self,data):
if self.inanchor:
print data # lets us print the anchor element inner data
self.inanchor = False # we handled the anchor element data
# this is not a nice way, self.inanchor should be set false

# when </a> is reached. try in end_a(self) ...
#def start_form(self, attrs):
# self.dump('form', attrs)

y = x(formatter.NullFormatter())
y.feed(urllib.urlopen('http://www.aquabase.org/fish/dump.php3').read())
y.close()

Jul 19 '05 #2

P: n/a
You should take a look at BeautifulSoup at:

http://www.python.org/pypi/BeautifulSoup/2.0.2

Larry Bates

raver2046 wrote:
here i have a link <a href="http://raver2046.ath.cx/CV/">cv network
admin</a>

how to extract "cv network admin"
here is the code i have find to exctract link but not title of link

----------------------------
import htmllib, formatter, urllib
class x(htmllib.HTMLParser):
def dump(self, tag, attrs):
#print tag,
for a, v in attrs:
if a in ['a', 'src', 'href']:
print v,

print
#def do_img(self, attrs):
# self.dump('img', attrs)
def start_a(self, attrs):
self.dump('a', attrs)
#def start_form(self, attrs):
# self.dump('form', attrs)

y = x(formatter.NullFormatter())
y.feed(urllib.urlopen('http://www.aquabase.org/fish/dump.php3').read())
y.close()
----------------------------

http://raver2046.ath.cx/CV/cv_fr.html

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.