While loop

Chris Rebert clp2 at rebertia.com
Thu Mar 5 13:54:29 EST 2009


On Thu, Mar 5, 2009 at 9:49 AM, 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?
<snip>
> 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?

You need to narrow the `try` and put it inside another while-loop:

while i<200:
    for domain in domains:
        srch.query = "test site:.%s" % domain
        while True:
            try:
                res = srch.parse_results()
            except SearchError:
                pass
            else:
                break
        print >> k, res.total_results_available
        i += 1

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list