1

I'm working with Twint(a Twitter scraper) but somehow there is a problem that I can't fix. I wonder if there is a way that when an error occurs, wait 1 min and re-execute this? My code is like this:

import twint

geos = ["40.74566208501717, -73.99137569478954", "35.68802408270403, 139.76489869554837", "31.22521968438549, 121.51655148017774"]
    
for geo in geos:
    print(str(geo)+','+'10km')
    c = twint.Config()
    c.Limit = 20
    c.Geo = str(geo)+','+'10km'
    twint.run.Search(c)

Sometimes,twint.run.Search(c) can't function correctly.So, once there is an error, is there a way to only execute this loop again but not re-executing the whole loop?

Would anyone help me? Any idea would be really helpful. Thank you much!

Haotian Ma
  • 21
  • 1
  • 4
  • What happens in the situation where it doesn't function correctly. Does it crash? – Sam Dec 16 '21 at 16:27

2 Answers2

2

If you want to simply pretend the error didn't happen, you could do:

try:
    twint.run.Search(c)
except WhateverExceptionType:
    pass

(replace WhateverExceptionType with the actual type of error you're seeing)

If when there's an error you wanted to have the whole program wait for a minute before continuing the loop, put that in the except:

import time

...

    try:
        twint.run.Search(c)
    except WhateverExceptionType:
        time.sleep(60)

If you want it to re-execute that specific search after waiting (rather than continuing with the next loop iteration), put that in the except. Note that if code within an except raises, then it will raise out of the except and stop your program.

    try:
        twint.run.Search(c)
    except WhateverExceptionType:
        time.sleep(60)
        twint.run.Search(c)
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

You could do something like this to try the search again after 60 sec and with say maximum 10 retries:

import time

for geo in geos:
    print(str(geo)+','+'10km')
    c = twint.Config()
    c.Limit = 20
    c.Geo = str(geo)+','+'10km'

    success=False
    retries = 0
    while not success and retries <= 10:
        try:
            twint.run.Search(c)
            success=True
        except twint.token.RefreshTokenException:
            time.sleep(60)
            retries += 1
        except: # <- catches all other exceptions
            retries = 11  # <- e.g. stop trying if another exception was raised
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75