Johhny
Johhny

Reputation: 47

for loop skipping over code?

So im trying to execute this code:

liner = 0
for eachLine in content:
    print(content[liner].rstrip())
    raw=str(content[liner].rstrip())
    print("Your site:"+raw)
    Sitecheck=requests.get(raw)
    time.sleep(5)
    var=Sitecheck.text.find('oht945t945iutjkfgiutrhguih4w5t45u9ghdgdirfgh')
    time.sleep(5)
    print(raw)
    liner += 1

I would expect this to run through the first print up to the liner variable and then go back up, however something else seems to happen:

https://google.com
Your site:https://google.com
https://google.com
https://youtube.com
Your site:https://youtube.com
https://youtube.com
https://x.com
Your site:https://x.com

This happens before the get requests. And the get requests later just get timed out:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))

I tried adding time.sleep(5) in my code for it to run smoother however this failed to yield results

Upvotes: 0

Views: 65

Answers (1)

Mr. T
Mr. T

Reputation: 12410

Why don't you use Python's exception handling, to catch failed connections?

import requests

#list with websites
content = ["https://google.com", "https://stackoverflow.com/", "https://bbc.co.uk/", "https://this.site.doesnt.exi.st"]
#get list index and element
for liner, eachLine in enumerate(content):
    #not sure, why this line exists, probably necessary for your content list
    raw = str(eachLine.rstrip())
    #try to get a connection and give feedback, if successful
    try:
        Sitecheck = requests.get(raw)
        print("Tested site #{0}: site {1} responded".format(liner, raw))
    except:
        print("Tested site #{0}: site {1} seems to be down".format(liner, raw))

Mind you that there are more elaborate ways like scrapy or beautifulsoup in Python to retrieve web content. But I think that your question is more a conceptual than a practical one.

Upvotes: 1

Related Questions