lokheart
lokheart

Reputation: 24675

Sorting list of beautifulsoup tag using python

I used the script below and extracted a list of url:

request = urllib2.Request("http://www.dummyurl.com")
pub_lv1 = urllib2.urlopen(request)
pub_lv1_parse = BeautifulSoup(pub_lv1)
pub_lv1_parse = pub_lv1_parse.body.find('table', attrs={"class":"proxy-archive-content-year-list"})
pub_lv1_parse = pub_lv1_parse.findAll('a')

The output is as below:

[<a href="/content/by/year/2011">2011</a>,
 <a href="/content/by/year/2012">2012</a>,
 <a href="/content/by/year/2013">2013</a>,
 <a href="/content/by/year/2000">2000</a>,
 <a href="/content/by/year/2001">2001</a>,
 <a href="/content/by/year/2002">2002</a>,
 <a href="/content/by/year/2003">2003</a>,
 <a href="/content/by/year/2004">2004</a>,
 <a href="/content/by/year/2005">2005</a>]

As you can see the year is not ordered, I want to sort them, I know how to sort a list of string using sort but what about output from beautifulsoup?

Upvotes: 3

Views: 2198

Answers (1)

falsetru
falsetru

Reputation: 369394

Sort by element text:

sorted(pub_lv1_parse, key=lambda elem: elem.text)

Upvotes: 5

Related Questions