Python:当条件发生时停止循环

时间:2018-02-21 05:26:04

标签: python loops beautifulsoup break scrape

我试图通过更改网址中的公里数来从网站上抓取数据。但问题是,每辆车都有最大的公里数,让我们说这个例子为900。由于我不知道它的最大值,我只是将范围设置为1000.它仍然可以打印出901的值,但值是"达到最大值"。我不需要不完整的值,那么如何在900之后停止循环?我为此使用了break,但它仍然提取数据直到循环结束。

代码:

for i in range(1000):
    lis=[]
    ne="kms=%d" %i
    url="http://www.xxxx.com/result.php?displacement=3&doors=1&{0}&rating_class=2".format(ne) 
    res= requests.get(url)
    soup=BeautifulSoup(res.text,'lxml')
    car = soup.select_one('.car-name')
    car_name=car.text
    car_name=car_name.replace('\n', '').replace('\t','')
    print(car_name)
    lis.append(car_name)

    ctype=soup.select_one('.car-type')
    car_type=ctype.text
    car_type=car_type.replace('\n', '').replace('\t','')
    car_type1=car_type.split('[')[1].split(',')[0]
    car_type2=car_type.split(', ')[1].split(']')[0]
    #print(car_type)
    print(car_type1)
    print(car_type2)
    lis.append(car_type1)
    lis.append(car_type2)

    for num in soup.find_all(attrs={'class':'result-Info-sec result-Info-sec-data'}):
        for value in num.find_all(['h1', 'h2']):
            print(value.text)
            newvalue=value.text
            newvalue=newvalue.replace('\n', '').replace('\t','')
            if newvalue=="reach to maximum": 
                break
            lis.append(newvalue)
    print(lis)

代码结果:

['A_car', 'Z series', '2020', '76.90', '49.61', '45.98~52.07', '70000', '90', '1531']  #900
['B_car', 'T series', '2020', '73.90', '49.66', '45.98~57.07', '80000', '80', '1534']  #901
['C_car', 'Z series', '2017', '99.90']  #902

我真正想要的是:(只有完整数据才能循环到900)

['A_car', 'Z series', '2020', '76.90', '49.61', '45.98~52.07', '70000', '90', '1531']  #900
['B_car', 'T series', '2020', '73.90', '49.66', '45.98~57.07', '80000', '80', '1534']  #901

#901(B_car)的HTML:

        <!-- info -->
        <div class="result-Info-sec result-Info-sec-data">
            <!-- up -->
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>new car:</h5>
                <h1 class="data-home data-home-01">
                    73.90                   </h1>
                <span>million</span>

            </div>
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>prediction:</h5>

                <h1 class="data-home data-home-01 formatSec">
                            49.66
                </h1>
                <span>million</span>


            </div>
            <div class="result-Info-sec-data-up result-Info-sec-data-cont">
                <h5>ranges:</h5>

                <h1 class="data-home data-home-02">
                            <b class="formatSec">45.98</b>~<b class="formatSec">52.07</b>
                </h1>
                <span>million</span>
                                            </div>

            <!-- line -->
            <div class="middle-line"></div>


            <!-- down -->
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>km:</h5>

                <h1 class="data-home data-home-03 thousandSec">
                80000
                </h1>
                <span>km</span>
                                            </div>
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>Accuracy:</h5>

                <h1 class="data-home data-home-02">
                    80
                </h1>
                <span>%</span>
                                            </div>
            <div class="result-Info-sec-data-up result-Info-sec-data-cont">
                <h5>view:</h5>

                <h1 class="data-home data-home-03">
                    1534
                </h1>
                <span>人</span>
                                            </div>

        </div>
                </div>

#902(C_Car)的HTML:

        <!-- info -->
        <div class="result-Info-sec result-Info-sec-data">
            <!-- up -->
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>new car:</h5>
                <h1 class="data-home data-home-01">
                    99.90                   </h1>
                <span>million</span>

            </div>
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>prediction:</h5>
                    <h2>reach to maximum</h2><p>maximum:900kms</p>

            </div>
            <div class="result-Info-sec-data-up result-Info-sec-data-cont">
                <h5>ranges:</h5>
                    <h2>reach to maximum</h2><p>maximum:900kms</p>              </div>

            <!-- line -->
            <div class="middle-line"></div>


            <!-- down -->
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>km:</h5>
                    <h2>reach to maximum</h2><p>maximum:900kms</p>              </div>
            <div class="result-Info-sec-data-up borderRight result-Info-sec-data-cont">
                <h5>Accuracy:</h5>
                    <h2>reach to maximum</h2><p>maximum:900kms</p>              </div>
            <div class="result-Info-sec-data-up result-Info-sec-data-cont">
                <h5>view:</h5>
                    <h2>reach to maximum</h2><p>maximum:900kms</p>              </div>

        </div>
                </div>

1 个答案:

答案 0 :(得分:1)

for i in range(1000):
    lis=[]
    ne="kms=%d" %i
    url="http://www.xxxx.com/result.php?displacement=3&doors=1&{0}&rating_class=2".format(ne) 
    res= requests.get(url)
    soup=BeautifulSoup(res.text,'lxml')
    car = soup.select_one('.car-name')
    car_name=car.text
    car_name=car_name.replace('\n', '').replace('\t','')
    print(car_name)
    lis.append(car_name)

    ctype=soup.select_one('.car-type')
    car_type=ctype.text
    car_type=car_type.replace('\n', '').replace('\t','')
    car_type1=car_type.split('[')[1].split(',')[0]
    car_type2=car_type.split(', ')[1].split(']')[0]
    #print(car_type)
    print(car_type1)
    print(car_type2)
    lis.append(car_type1)
    lis.append(car_type2)
    need_to_stop=False
    for num in soup.find_all(attrs={'class':'result-Info-sec result-Info-sec-data'}):
        for value in num.find_all(['h1', 'h2']):
            print(value.text)
            newvalue=value.text
            newvalue=newvalue.replace('\n', '').replace('\t','')
            if newvalue=="reach to maximum":
                need_to_stop=True 
                break
            lis.append(newvalue)
        if need_to_stop:
            break
    print(lis)
    if need_to_stop:
        break

希望这有帮助