I am trying to open a page/link and catch the content in it. It gives me the required content sometimes and throws error sometimes. I see that if I refresh the page a few times - I get the content.
So, I want to reload the page and catch it.
Here's my pseudo code:
attempts = 0
while attempts:
try:
open_page = urllib2.Request(www.xyz.com)
# Or I think we can also do urllib2.urlopen(www.xyz.com)
break
except:
# here I want to refresh/reload the page
attempts += 1
My questions are:
1. How can I reload the page using urllib or urllib2 or requests or mechanize?
2. Can we loop try catch that way?
Thank you!
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
attempts = 10
retries = Retry(total=attempts,
backoff_factor=0.1,
status_forcelist=[ 500, 502, 503, 504 ])
sess = requests.Session()
sess.mount('http://', HTTPAdapter(max_retries=retries ))
sess.mount('https://', HTTPAdapter(max_retries=retries))
sess.get('http://www.google.co.nz/')