Hack Forums
[Python] If website takes too long time to load - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] If website takes too long time to load (/showthread.php?tid=5931079)



If website takes too long time to load - Amanththonania - Jan 18, 2019

Hi.

I'm making a program that reads a specific site and then it writes the whole site to a .txt file.
I want to make it so if the website takes more than 1 second to load it skips and goes to the next site.

My code looks something like this:

Code
import urllib.request
Number = 123
url = "http:/website.com/number={}"

while True:
    read = urllib.request.urlopen(url.format(x)).read()
    
    text_file = open("text.txt", "ab")
    text_file.write(read)
    text_file.close()
    print(read)
    Number = Number + 1



RE: If website takes too long time to load - lockhead - Jan 18, 2019

Something like this should work.
http://docs.python-requests.org/en/master/user/quickstart/#timeouts

Code
import requests

while True:
    #set timeout in seconds
    try:
        requests.get('https://website.com', timeout=0.05)
    except requests.exceptions.Timeout as e:
        print("Website timed out")



RE: If website takes too long time to load - Amanththonania - Jan 19, 2019

Thanks works perfect! :)

Sent u some bytes