如何运行整个for循环(永远循环)来获取整个表记录?

时间:2014-08-19 14:03:15

标签: python

如何运行整个for循环来获取整个表记录,而不是永久循环只获得第一个记录

代码:

import time
import urllib2
from bs4  import BeautifulSoup

while True:
  contenturl = "http://www.bank.gov.ua/control/en/curmetal/detail/currency?period=daily"
  soup = BeautifulSoup(urllib2.urlopen(contenturl).read())

  table = soup.find('div', attrs={'class': 'content'})

  rows = table.findAll('tr')
  for tr in rows:
    cols = tr.findAll('td')
    if 'cell_c' in cols[0]['class']:
        # currency row
        digital_code, letter_code, units, name, rate = [c.text for c in cols]
        print digital_code, letter_code, units, name, rate
        break
        time.sleep(1)

代码输出:(重复循环只是第一个数据)

036 AUD 100 Australian Dollar 1207.1332  
036 AUD 100 Australian Dollar 1207.1332
036 AUD 100 Australian Dollar 1207.1332
036 AUD 100 Australian Dollar 1207.1332
036 AUD 100 Australian Dollar 1207.1332
036 AUD 100 Australian Dollar 1207.1332
036 AUD 100 Australian Dollar 1207.1332
036 AUD 100 Australian Dollar 1207.1332
036 AUD 100 Australian Dollar 1207.1332

预期输出:(重复循环以获取整个数据)

036 AUD 100 Australian Dollar 1207.1332
944 AZN 100 Azerbaijanian Manat 1651.6929
974 BYR 10 Belarussian Ruble 0.0125
124 CAD 100 Canadian Dollar 1190.6335
203 CZK 100 Czech Koruna 62.1306
208 DKK 100 Danish Krone 232.5053
978 EUR 100 EURO 1733.4432
348 HUF 1000 Forint 55.2263
352 ISK 100 Iceland Krona 11.2017
440 LTL 100 Lithuanian Litas 502.0399
934 TMT 100 Manat 454.4764
498 MDL 100 Moldovan Leu 94.3694
578 NOK 100 Norwegian Krone 210.5866
826 GBP 100 Pound Sterling 2166.2625
643 RUB 10 Russian ruble 3.5950
960 XDR 100 SDR 1981.8556
702 SGD 100 Singapore Dollar 1040.5446
752 SEK 100 Swedish Krona 189.1724
756 CHF 100 Swiss Franc 1431.7694
398 KZT 100 Tenge 7.1168
949 TRY 100 Turkish Lira 597.4506
840 USD 100 US Dollar 1295.2576
860 UZS 100 Uzbekistan Sum 0.5525
392 JPY 1000 Yen 126.3258
156 CNY 100 Yuan Renminbi 210.7094
985 PLN 100 Zloty 413.8775

036 AUD 100 Australian Dollar 1207.1332
944 AZN 100 Azerbaijanian Manat 1651.6929
974 BYR 10 Belarussian Ruble 0.0125
124 CAD 100 Canadian Dollar 1190.6335
203 CZK 100 Czech Koruna 62.1306
208 DKK 100 Danish Krone 232.5053
978 EUR 100 EURO 1733.4432
348 HUF 1000 Forint 55.2263
352 ISK 100 Iceland Krona 11.2017
440 LTL 100 Lithuanian Litas 502.0399
934 TMT 100 Manat 454.4764
498 MDL 100 Moldovan Leu 94.3694
578 NOK 100 Norwegian Krone 210.5866
826 GBP 100 Pound Sterling 2166.2625
643 RUB 10 Russian ruble 3.5950
960 XDR 100 SDR 1981.8556
702 SGD 100 Singapore Dollar 1040.5446
752 SEK 100 Swedish Krona 189.1724
756 CHF 100 Swiss Franc 1431.7694
398 KZT 100 Tenge 7.1168
949 TRY 100 Turkish Lira 597.4506
840 USD 100 US Dollar 1295.2576
860 UZS 100 Uzbekistan Sum 0.5525
392 JPY 1000 Yen 126.3258
156 CNY 100 Yuan Renminbi 210.7094
985 PLN 100 Zloty 413.8775

2 个答案:

答案 0 :(得分:1)

我认为问题在于您在找到第一个货币行后break。这突破了遍历所有行的for循环,您看到的重复行来自while循环,该循环设置为永远循环,因为条件总是{{ 1}}。

答案 1 :(得分:0)

为什么你恰好在那个while循环中?

contenturl = "http://www.bank.gov.ua/control/en/curmetal/detail/currency?period=daily"
soup = BeautifulSoup(urllib2.urlopen(contenturl).read())
table = soup.find('div', attrs={'class': 'content'})
rows = table.findAll('tr')
for row in rows:
    print row.text.split()