Python 用于循环跳过代码?

Python 用于循环跳过代码?,python,for-loop,python-requests,Python,For Loop,Python Requests,因此,我尝试执行以下代码: 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('oht945t945iutjkfgiutrh

因此,我尝试执行以下代码:

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
我希望它在第一次打印到liner变量时运行,然后再返回,但是似乎发生了其他事情:

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
这发生在get请求之前。稍后的get请求将超时:

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',))

我尝试在代码中添加time.sleep(5)以使其运行更顺畅,但这并没有产生结果

为什么不使用Python的异常处理来捕获失败的连接

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))
请注意,在Python中有更复杂的方法,如
scrapy
beautifulsoup
,来检索web内容。但我认为你的问题更多的是一个概念性的问题,而不是一个实际的问题