[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Parsing links within a html file.

Shriphani

1/14/2008 6:00:00 PM

Hello,
I have a html file over here by the name guide_ind.html and it
contains links to other html files like guides.html#outline . How do I
point BeautifulSoup (I want to use this module) to
guides.html#outline ?
Thanks
Shriphani P.
1 Answer

Hai Vu

1/17/2008 7:49:00 AM

0

On Jan 14, 9:59 am, Shriphani <shripha...@gmail.com> wrote:
> Hello,
> I have a html file over here by the name guide_ind.html and it
> contains links to other html files like guides.html#outline . How do I
> point BeautifulSoup (I want to use this module) to
> guides.html#outline ?
> Thanks
> Shriphani P.

Try Mark Pilgrim's excellent example at:
http://www.diveintopython.org/http_web_services/...

From the above link, you can retrieve openanything.py which I use in
my example:

# list_url.py
# created by Hai Vu on 1/16/2008

from openanything import fetch
from sgmllib import SGMLParser

class RetrieveURLs(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.urls = []

def start_a(self, attributes):
url = [v for k, v in attributes if k.lower() == 'href']
self.urls.extend(url)
print '\t%s' % (url)

#
--------------------------------------------------------------------------------------------------------------
# main
def main():
site = 'http://www.googl...

result = fetch(site)
if result['status'] == 200:
# Extracts a list of URLs off the top page
parser = RetrieveURLs()
parser.feed(result['data'])
parser.close()

# Display the URLs we just retrieved
print '\nURL retrieved from %s' % (site)
print '\t' + '\n\t'.join(parser.urls)
else:
print 'Error (%d) retrieving %s' % (result['status'], site)

if __name__ == '__main__':
main()