2 Действительные URL, запросы.get() завершается с ошибкой 1, но не с другой. Почему?

Я запускаю скрипт для проверки связки ссылок из БД. Хотя куча сбоев на самом деле является действительными ссылками, но я не могу понять, почему они терпят неудачу и есть ли способ заставить их работать.

Я новичок в этом, я пробовал несколько вещей, разные заголовки, без заголовка, больше времени ожидания, нет времени ожидания. Не уверен, что попробовать дальше. Я запускаю это на компьютере с Windows 10, через прокси-сервер, прокси был настроен в файле настроек пользователя.

Вот тестовый код, первый URL не работает, второй работает.

# For handling the requests to the webpages
import requests
from requests_negotiate_sspi import HttpNegotiateAuth


# Test results, 1 record per URL to test
w = open(r'C:\Temp\URL_Test_Results.txt', 'w')

# For errors only
err = open(r'C:\Temp\URL_Test_Error_Log.txt', 'w')

print('Starting process')


def test_url(url):
    # Test the URL and write the results out to the log files.

    # Had to disable the warnings, by turning off the verify option, a warning is generated as the
    # website certificates are not checked, so results could be "bad". The main site throws errors
    # into the log for each test if we don't turn it off though.
    requests.packages.urllib3.disable_warnings()
    headers={'User-Agent': 'Mozilla/5.0 (X11; OpenBSD i386) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'}
    print('Testing ' + url)
    # Try the website link, check for errors.
    try:
        response = requests.get(url, auth=HttpNegotiateAuth(), verify=False, headers=headers, timeout=5)
    except requests.exceptions.HTTPError as e:
        print('HTTP Error')
        print(e)
        w.write('HTTP Error, check error log' + '\n')
        err.write('HTTP Error' + '\n' + url + '\n' + e + '\n' + '***********' + '\n' + '\n')
    except requests.exceptions.ConnectionError as e:
        # some external sites come through this, even though the links work through the browser
        # I suspect that there's some blocking in place to prevent scraping...
        # I could probably work around this somehow.
        print('Connection error')
        print(e)
        w.write('Connection error, check error log' + '\n')
        err.write(str('Connection Error') + '\n' + url + '\n' + str(e) + '\n' + '***********' + '\n' + '\n')
    except requests.exceptions.RequestException as e:
        # Any other error types
        print('Other error')
        print(e)
        w.write('Unknown Error' + '\n')
        err.write('Unknown Error' + '\n' + url + '\n' + e + '\n' + '***********' + '\n' + '\n')
    else:
        # Note that a 404 is still 'successful' as we got a valid response back, so it comes through here
        # not one of the exceptions above.
        response = requests.get(url, auth=HttpNegotiateAuth(), verify=False)
        print(response.status_code)
        w.write(str(response.status_code) + '\n')
        print('Success! Response code:', response.status_code)
    print('========================')


test_url('https://www.abs.gov.au/websitedbs/D3310114.nsf/home/census')
test_url('https://www.statista.com/')

print('Done!')
w.close()
err.close()

Первый URL не может соединиться, второй возвращает код 200, но обе ссылки работают для меня через браузер.

Мой журнал выглядит так:

Starting process
Testing https://www.abs.gov.au/websitedbs/D3310114.nsf/home/census
Connection error
HTTPSConnectionPool(host='www.abs.gov.au', port=443): Max retries exceeded with url: /websitedbs/D3310114.nsf/home/census (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x000001310B30B4E0>, 'Connection to www.abs.gov.au timed out. (connect timeout=5)'))
========================
Testing https://www.statista.com/
200
Success! Response code: 200
========================
Done!

0 ответов

Другие вопросы по тегам