使用python排序beautifulsoup标签的列表

我使用下面的脚本并提取了网址列表:

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')

输出如下:

[<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>]

如您所见,年份未排序,我想对它们进行排序,我知道如何使用sort对字符串列表进行排序,但是beautifulsoup的输出又如何呢?

解决方法:

按元素文字排序:

sorted(pub_lv1_parse, key=lambda elem: elem.text)
上一篇:[Java Spring] Built-in page and sorting


下一篇:java-使用compareTo和Collections.sort