简体   繁体   English

requests.exceptions.ConnectionError Python

[英]requests.exceptions.ConnectionError Python

I have problem couse i need find bad urls of pictures its my script:我有问题,因为我需要找到错误的图片网址,这是我的脚本:

import requests
import csv
import time
with open(nazwa_pliku) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=';')
    count=0
    mapa = []
    id = 1
    next(csv_reader)
    next(csv_reader)

    for row in csv_reader:
        if row[1] != "":
            ID=row[0]
            NUMBER=row[1]
            PICTURES=row[2].split('|')


            for url in PICTURES:
                url="https://sw67383.mywebshop.io/upload_dir/shop/"+url

                result = requests.get(url, stream=True)
                if result.status_code != 200:

                    print(colored("Brak: ", "red"), url)
                    object = {
                        "PRODUCT_ID": ID,
                        "NUMBER":NUMBER,
                        "PHOTO":url,

                    }
                    count += 1
                    mapa.append(object)
                else:
                    print(colored(str(id)+" Poprawny: ", "green"), url)
                id+=1

    print(colored("Liczba Brakujących zdjęć: ", "yellow")+"{}/{}").format(count,id)
    return mapa  

 

For example i get it from csv files and I request urls but some times i have connection error i dont know why.例如,我从 csv 文件中获取它并且我请求 url 但有时我有连接错误,我不知道为什么。 Maybe my internet or server.也许我的互联网或服务器。

and i getting error我得到错误

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='sw67383.mywebshop.io', port=443): Max retries exceeded with url: /upload_dir/shop/maxtone/MAXTON_4306_4.jpg (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object..... requests.exceptions.ConnectionError: HTTPSConnectionPool(host='sw67383.mywebshop.io', port=443): url: /upload_dir/shop/maxtone/MAXTON_4306_4.jpg (由 NewConnectionError('<urllib3.connection.已验证HTTPS连接object.....

What can i do to avoid this problem我能做些什么来避免这个问题

I need to check 3000 urls of pictures.我需要检查 3000 个图片的 url。 And in future much more.并且在未来更多。

EDIT: I change it like编辑:我改变它喜欢

   for row in csv_reader:
        if row[1] != "":
            ID=row[0]
            NUMBER=row[1]
            PICTURES=row[2].split('|')


            for url in PICTURES:
                url="https://sw67383.mywebshop.io/upload_dir/shop/"+url


                try:
                    result = requests.get(url, stream=True)
                    if result.status_code != 200:

                        print(colored("Brak: ", "red"), url)
                        object = {
                            "PRODUCT_ID": ID,
                            "NUMBER": NUMBER,
                            "PHOTO": url,

                        }
                        count += 1
                        mapa.append(object)
                    else:
                        print(colored(str(id) + " Poprawny: ", "green"), url)
                    id += 1
                except requests.ConnectionError:
                    print("Problem z połączeniem z adresem: {} ".format(url))

And now i know when is "time out" but not good when it will bad link to picture (404):P so maybe i shoud save this to object too?现在我知道什么时候“超时”,但不好的时候它会坏链接到图片(404):P所以也许我也应该把它保存到 object 吗? and manual verify link like its correct url or wrong并手动验证链接,如正确的 url 或错误

Ok I foud it how I can avoid problem:好的,我发现了如何避免问题:

 for url in PICTURES:
                    url="https://sw67383.mywebshop.io/upload_dir/shop/"+url
                    session = requests.Session()
                    retry = Retry(connect=3, backoff_factor=0.5)
                    adapter = HTTPAdapter(max_retries=retry)
                    session.mount('http://', adapter)
                    session.mount('https://', adapter)

                    result = session.get(url)
                    if result.status_code != 200:

                        print(colored("Brak: ", "red"), url)
                        object = {
                                "PRODUCT_ID": ID,
                                "NUMBER": NUMBER,
                                "PHOTO": url,
                                "COMMUNICATE":"BRAK"

                            }
                         count += 1
                         mapa.append(object)

                     else:
                          print(colored(str(id) + " Poprawny: ", "green"), url)
                     id += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:requests.exceptions.ConnectionError:HTTPSConnectionPool - Python: requests.exceptions.ConnectionError: HTTPSConnectionPool 意外的请求。异常。连接错误 - Unexpected requests.exceptions.ConnectionError requests.exceptions.ConnectionError:HTTPConnectionPool - requests.exceptions.ConnectionError: HTTPConnectionPool 如何解决获取请求中的 requests.exceptions.ConnectionError - Python - how to solve requests.exceptions.ConnectionError in get request - Python python - 处理连接异常(requests.exceptions.ConnectionError) - python - handle connection exception (requests.exceptions.ConnectionError) requests.exceptions.ConnectionError [Errno 11004] getaddrinfo在Python中使用请求失败。 浏览器工作 - requests.exceptions.ConnectionError [Errno 11004] getaddrinfo failed using Requests in Python. Browser works POST API 请求如何在 python 中使用并行处理? requests.exceptions.ConnectionError: - how do POST API requests use parallel processing in python? requests.exceptions.ConnectionError: Python request.exceptions.ConnectionError:HTTPConnectionPool(host = &#39;10 .10.10.10&#39;,port = 80):URL超过了最大重试次数 - Python requests.exceptions.ConnectionError: HTTPConnectionPool(host='10.10.10.10', port=80): Max retries exceeded with url Python request.exceptions.ConnectionError:HTTPSConnectionPool:URL超过最大重试次数:[Errno 111]连接被拒绝) - Python requests.exceptions.ConnectionError: HTTPSConnectionPool : Max retries exceeded with url: [Errno 111] Connection refused) Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,)) - Python: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(“(54, 'ECONNRESET')”,))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM