我有一个来自links = set(soup.findAll('a'))
的列表,现在我想对它们进行排序,但是links = sorted(links)
出现错误,该怎么排序?
原始列表和其他所有属性:
<a href="//weathernews.jp/typhoon/">台風</a>
<a class="list" href="https://weathernews.jp/s/topics/201809/190035/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/130155/?fm=tp_index"></a>
<a href="//weathernews.jp/warning/">警報・注意報</a>
<a class="list" href="https://weathernews.jp/s/topics/201809/140125/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/170145/?fm=tp_index"></a>
我想对它进行排序
<a class="list" href="https://weathernews.jp/s/topics/201809/190035/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/170145/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/140125/?fm=tp_index"></a>
<a class="list" href="https://weathernews.jp/s/topics/201809/130155/?fm=tp_index"></a>
<a href="//weathernews.jp/typhoon/">台風</a>
<a href="//weathernews.jp/warning/">警報・注意報</a>
答案 0 :(得分:1)
我建议您先将链接映射到字符串,然后对其进行排序。
links = set(soup.findAll('a'))
links = map(str, links)
links = sorted(links)
或
简单地
links = sorted(set(map(str, links)))
答案 1 :(得分:0)
我明白了
links = sorted(links, key=lambda x: str(x), reverse=False)