【问题标题】:Extract unique elements from a list returned by BeautifulSoup从 BeautifulSoup 返回的列表中提取唯一元素
【发布时间】:2014-12-07 12:51:11
【问题描述】:

我有从 this 网站上抓取的国家/地区列表,其值类似于

(注意:这是all_countries在迭代其元素后的输出)

<a  data-flexible="" SELECT data-id="AU" href="http://www.wotif.com/AU">Australia</a>
<a  data-flexible="" data-id="NZ" href="http://www.wotif.com/NZ">New Zealand</a>
<a  data-flexible="" data-id="ID" href="http://www.wotif.com/ID">Indonesia</a>
<a  data-flexible="" data-id="TH" href="http://www.wotif.com/TH">Thailand</a>
<a  data-flexible="" data-id="SG" href="http://www.wotif.com/SG">Singapore</a>
<a  data-flexible="" data-id="GB" href="http://www.wotif.com/GB">United Kingdom</a>
<a  data-flexible="" data-id="TH" href="http://www.wotif.com/TH">Thailand</a>
<a  data-flexible="" data-id="AU" href="http://www.wotif.com/AU">Australia</a>
<a  data-flexible="" data-id="AR" href="http://www.wotif.com/AR">Argentina</a>
<a  data-flexible="" data-id="NZ" href="http://www.wotif.com/NZ">New Zealand</a>

我想做的是获得唯一独特的国家

这是我尝试过的。

all_countries = countries.select('div#country-box ul li a')

for index,value in enumerate(all_countries):
    print(value)
    all_countries[index] = value.text

all_countries = set(all_countries)
all_countries = list(all_countries)

for index,value in enumerate(all_countries):
    print(value)

嗯,好吧,我现在有独特的元素,但它不保持这些国家的顺序,因为它们出现在 MultiSelectList 中,我还需要属性值 data-idhref 以及 a 的文本标记供以后在我的脚本中使用。

如果我这样做了

all_countries = countries.select('div#country-box ul li a')
all_countries = set(all_countries)

all_countries = list(all_countries)

这是一个好方法吗?

【问题讨论】:

  • 预期输出是什么?
  • 所以我得到了基于 data-id 值的唯一元素
  • @falsetru 如果我这样做all_countries = list(collections.OrderedDict.fromkeys(all_countries)),那么我将拥有澳大利亚 2 次,因为它的标记不同。这就是为什么我想根据data-id 值获得独一无二的原因
  • 看看我的回答。

标签: python python-3.x beautifulsoup


【解决方案1】:

使用set 存储已经看到的data-ids。

from bs4 import BeautifulSoup


def iter_uniq_link(all_countries):
    seen = set()
    for c in all_countries:
        data_id = c.get('data-id')
        if data_id not in seen:
            seen.add(data_id)
            yield c

用法:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''
... <body>
...     <div id="country-box">
...         <ul>
...             <li>
...                 <a  data-flexible="" SELECT data-id="AU" href="http://www.wotif.com/AU">Australia</a>
...                 <a  data-flexible="" data-id="NZ" href="http://www.wotif.com/NZ">New Zealand</a>
...                 <a  data-flexible="" data-id="ID" href="http://www.wotif.com/ID">Indonesia</a>
...                 <a  data-flexible="" data-id="TH" href="http://www.wotif.com/TH">Thailand</a>
...                 <a  data-flexible="" data-id="SG" href="http://www.wotif.com/SG">Singapore</a>
...                 <a  data-flexible="" data-id="GB" href="http://www.wotif.com/GB">United Kingdom</a>
...                 <a  data-flexible="" data-id="TH" href="http://www.wotif.com/TH">Thailand</a>
...                 <a  data-flexible="" data-id="AU" href="http://www.wotif.com/AU">Australia</a>
...                 <a  data-flexible="" data-id="AR" href="http://www.wotif.com/AR">Argentina</a>
...                 <a  data-flexible="" data-id="NZ" href="http://www.wotif.com/NZ">New Zealand</a>
...             </li>
...         </ul>
...     </div>
... </body>
... ''')
>>> all_countries = soup.select('div#country-box ul li a')
>>> list(iter_uniq_link(all_countries))
[<a data-flexible="" data-id="AU" href="http://www.wotif.com/AU" select="">Australia</a>,
 <a data-flexible="" data-id="NZ" href="http://www.wotif.com/NZ">New Zealand</a>,
 <a data-flexible="" data-id="ID" href="http://www.wotif.com/ID">Indonesia</a>,
 <a data-flexible="" data-id="TH" href="http://www.wotif.com/TH">Thailand</a>,
 <a data-flexible="" data-id="SG" href="http://www.wotif.com/SG">Singapore</a>,
 <a data-flexible="" data-id="GB" href="http://www.wotif.com/GB">United Kingdom</a>,
 <a data-flexible="" data-id="AR" href="http://www.wotif.com/AR">Argentina</a>]

【讨论】:

    【解决方案2】:

    保持顺序和唯一性的一种可能方法是使用 OrderedDict。将data-id 的每个唯一值作为键添加到 OrderedDict。

    https://docs.python.org/3.3/library/collections.html#collections.OrderedDict

    在这样的字典中添加键将在您遍历它时保留它们的插入顺序(例如使用.keys())。

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多