我想使用python 3.4

时间:2016-09-02 05:58:49

标签: python html html5 python-3.4

我想在for循环中一次性打印特定的但是在一行中输出结果它给出相同的结果四次请帮我如何在打印一行后停止循环

这里有完整的html和python代码以及此脚本的结果

<ul class="breadcrumbs" id="BREADCRUMBS">
  <li class="breadcrumb_item " itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
   <a class="breadcrumb_link" href="/Tourism-g191-United_States-Vacations.html" itemprop="url" onclick="ta.setEvtCookie('Breadcrumbs', 'click', 'Country', 1, this.href); ">
       <span itemprop="title">United States</span>
   </a>
   <span class="separator">›</span>
  </li>
    .
    .
    .
    .
打印结果

的Python脚本

ulpart = soup.find_all("ul", {"class": "breadcrumbs"})
    			for unorder in ulpart:
    				div2 = soup.find_all("li", {"class": "breadcrumb_item "})
    				for listitem in div2[0:]:
    					country = soup.select_one("li.breadcrumb_item a[onclick*=Country]").get_text(strip=True)
    					print(country)

这是四次打印相同结果的代码的结果

United State
United State
United State
United State

但我希望美国只有这样一次:

United State

3 个答案:

答案 0 :(得分:0)

printed_countries = list()
ulpart = soup.find_all("ul", {"class": "breadcrumbs"})
                for unorder in ulpart:
                    div2 = soup.find_all("li", {"class": "breadcrumb_item "})
                    for listitem in div2[0:]:
                        country = soup.select_one("li.breadcrumb_item a[onclick*=Country]").get_text(strip=True)
                        if not country in printed_countries:
                            print(country)
                            printed_countries.append(country)

答案 1 :(得分:0)

由于您使用的是无序列表,因此可以使用python set datatype(如果需要使用订单使用列表):

printed = set()

ulpart = soup.find_all("ul", {"class": "breadcrumbs"})
                for unorder in ulpart:
                    div2 = soup.find_all("li", {"class": "breadcrumb_item "})
                    for listitem in div2[0:]:
                        country = soup.select_one("li.breadcrumb_item a[onclick*=Country]").get_text(strip=True)
                        printed.add(country)

答案 2 :(得分:0)

printed = set()

ulpart = soup.find_all("ul", {"class": breadcrumbs})
            for unorder in ulpart:
                div2 = soup.find_all("li", {"class": "breadcrumb_item "})
                for listitem in div2[0:]:
                    country = soup.select_one("li.breadcrumb_item 
a[onclick*=Country]").get_text(strip=True)
                    print.add(country)

这似乎是伙伴