for循环跳过代码?

时间:2018-03-11 03:56:23

标签: 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('oht945t945iutjkfgiutrhguih4w5t45u9ghdgdirfgh')
    time.sleep(5)
    print(raw)
    liner += 1

我希望这可以通过第一次打印到衬里变量然后再回来,但是似乎还会发生其他事情:

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请求之前。以后获取请求只会超时:

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)以使其运行更顺畅但是这无法产生结果

1 个答案:

答案 0 :(得分:1)

为什么不使用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中有更复杂的方法,例如scrapybeautifulsoup来检索网页内容。但我认为你的问题更像是概念而非实际问题。