[英]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.