我无法检查第二个和第三个URL的状态,并且无法访问第四个URL。
我试图使用条件的但失败了-request.exceptions.TooManyRedirects :,&requests.exceptions.ConnectionError:
这是代码-
import requests
for url in ['google.com', 'cchgroup.com', 'skypeassets.com', 'yahoo.com']:
http = ("http://")
url2 = (http + url)
page = requests.get(url2)
#page.max_redirects = 60
if page.status_code == 200:
print('Success')
elif page.status_code == 404:
print('Not Found')
elif requests.exceptions.TooManyRedirects:
print('Exceeded 30 redirects')
elif requests.exceptions.ConnectionError:
print('Failed to establish a new connection')
else:
print('URL cannot be found')
但出现错误。
答案 0 :(得分:0)
'cchgroup'和'skypeassets.com'引发异常。您应该使用try / except块来处理它们。 这应该对您有用
import requests
for url in ['google.com', 'cchgroup.com', 'skypeassets.com', 'yahoo.com']:
http = ("http://")
url2 = (http + url)
#page.max_redirects = 60
try:
page = requests.get(url2)
if page.status_code == 200:
print('Success')
elif page.status_code == 404:
print('Not Found')
except requests.exceptions.TooManyRedirects:
print('Exceeded 30 redirects')
except requests.exceptions.ConnectionError:
print('Failed to establish a new connection')