While loop
Andre Engels
andreengels at gmail.com
Thu Mar 5 13:54:15 EST 2009
On Thu, Mar 5, 2009 at 6:49 PM, Fab86 <fabien.hall at gmail.com> wrote:
> On Mar 5, 5:23 pm, Marco Mariani <ma... at sferacarta.com> wrote:
>> Fab86 wrote:
>> > Is it possible to get the program to catch the exception, wait 10
>> > seconds, then carry of from where it was rather than starting again?
>>
>> something like this? probably works in PASCAL as well :)
>>
>> > i=0
>> > while i < len(stuff):
>> > try:
>> > do_with(stuff[i])
>> > except SomeError:
>> > sleep(10)
>> > continue
>> > i+=1
>>
>>
>
> using sleep and then continue just makes the search start from the
> first search term like before.. Would it be easier to understand if I
> posted sections of my code?
>
> i = 0
> k = open('blah', 'w')
> domains = ["au", "ca", "nl", "be", "...]
> srch = WebSearch(app_id=YahooKey)
>
> while i<200:
> try:
> for domain in domains:
> srch.query = "test site:.%s" % domain
> res = srch.parse_results()
> print >> k, res.total_results_available
> i = i + 1
>
> except SearchError:
>
> (I currently close then reopen document here then restart i to 0)
>
> Any ideas?
What you did not tell us was the inside loop. Just put code like
Tino's inside this inner loop:
while i<200:
for domain in domains:
srch.query = "test site:.%s" % domain
while true:
try:
res = srch.parse_results()
break
except SearchError:
time.sleep(10)
print >> k, res.total_results_available
i = i + 1
Or, if you want to to retry a maximum number of times:
max_retries = 50
while i<200:
for domain in domains:
srch.query = "test site:.%s" % domain
retries = 0
while true:
try:
res = srch.parse_results()
break
except SearchError:
if retries < max_retries:
time.sleep(10)
retries += 1
else:
raise TooManyRetriesError
print >> k, res.total_results_available
i = i + 1
--
André Engels, andreengels at gmail.com
More information about the Python-list
mailing list