python - 使用 python 对 beautifulsoup 标签列表进行排序

标签 python list sorting beautifulsoup

我使用了下面的脚本并提取了一个 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')

输出如下:

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

如您所见,year 没有排序,我想对它们进行排序,我知道如何使用 sort 对字符串列表进行排序,但是 的输出呢? code>beautifulsoup?

最佳答案

按元素文本排序:

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

关于python - 使用 python 对 beautifulsoup 标签列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17206616/

相关文章:

python - 同时在两个数据框中应用 lambda 函数

javascript - 更改列表中的文本

perl - 在 Perl 中排序捷克语

Python:如何在数字初级排序中执行二级降序字母排序

angularjs - 使用空对象进行 Angular 排序

python - 已安装 libusb - 但未找到 pyUSB 后端

python - 重复的 Django 查询集?

python 使用嵌套列表检查列表

python - 将列表写入文件以供日后重用

python - 如何让 python 计时器更改变量值