Load website's content through python
Question: How can i load a specific content from a website through python?For example,i
want to load some posts of a blog and appear them to my own site.How can i do
this?


Answer: An answer:

    
    
    import urllib2
    from BeautifulSoup import BeautifulSoup
    
    def fetchtags(req, name, attrs, num):
            try:
                website = urllib2.urlopen(req)
            except urllib2.HTTPError, e:
                print 'A problem occured. Please try again.'
                return
            soup = BeautifulSoup(website,
                                 convertEntities=BeautifulSoup.HTML_ENTITIES)
            tags = soup.findAll(name=name,
                                attrs=attrs,
                                limit=num)
            return tags
    

Then you can use it like:

    
    
    fetchtags('http://www.website.com', 'div', {'class':'c'}, 10)
    

To get 10 divs of class c from the specified url...

See Beautiful Soup for more details on the returned object.

