发生异常: ConnectionError HTTPConnectionPool(host='jpg', port=80): Max retries exceeded with…
- User
发生异常: ConnectionError HTTPConnectionPool(host='jpg', port=80): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x0000016333672620>: Failed to resolve 'jpg' ([Errno 11001] getaddrinfo failed)")) socket.gaierror: [Errno 11001] getaddrinfo failed
The above exception was the direct cause of the following exception:
urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPConnection object at 0x0000016333672620>: Failed to resolve 'jpg' ([Errno 11001] getaddrinfo failed)
The above exception was the direct cause of the following exception:
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='jpg', port=80): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x0000016333672620>: Failed to resolve 'jpg' ([Errno 11001] getaddrinfo failed)"))
During handling of the above exception, another exception occurred:
File "F:\maomaochong\caterpillar\test.py", line 18, in download_image response = requests.get(url) File "F:\maomaochong\caterpillar\test.py", line 46, in download_image(image_url, folder_path) requests.exceptions.ConnectionError: HTTPConnectionPool(host='jpg', port=80): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x0000016333672620>: Failed to resolve 'jpg' ([Errno 11001] getaddrinfo failed)")) 帮我翻译一下
- Assistant
出现异常:ConnectionError HTTPConnectionPool(host ='jpg',port = 80):最大重试次数超过了URL:/(由NameResolutionError("<urllib3.connection.HTTPConnection object at 0x0000016333672620>引起:无法解决'jpg'([Errno 11001] getaddrinfo 失败)“)”)
上面的异常是以下异常的直接原因:
urllib3.exceptions.NameResolutionError:<urllib3.connection.HTTPConnection object at 0x0000016333672620>:未能解析'jpg'([Errno 11001] getaddrinfo failed)
上面的异常是以下异常的直接原因:
urllib3.exceptions.MaxRetryError:HTTPConnectionPool(host ='jpg',port = 80):最大重试次数超过了URL:/(由NameResolutionError("<urllib3.connection.HTTPConnection object at 0x0000016333672620>引起:无法解决'jpg'([Errno 11001] getaddrinfo失败)“)”)
在处理上述异常时,发生了另一个异常:
在“F:\ maomaochong \ caterpillar \ test.py”中的第18行中,执行download_image 响应= requests.get(url) 在“F:\ maomaochong \ caterpillar \ test.py”的第46行中,以module为名, download_image(image_url,folder_path) requests.exceptions.ConnectionError:HTTPConnectionPool(host ='jpg',port = 80):最大重试次数超过了URL:/(由NameResolutionError("<urllib3.connection.HTTPConnection object at 0x0000016333672620>引起:无法解决'jpg'([Errno 11001] getaddrinfo失败)“)”)
- User
就这些吗
- Assistant
是的,以上是这段代码抛出的异常和错误信息的翻译。
- User
import requests from bs4 import BeautifulSoup import re import os
创建下载图片函数
def download_image(url, folder_path): if not url.startswith("http://") and not url.startswith("https://"): url = "http://" + url
# 获取文件名 filename = url.split("/")[-1] # 构造本地文件路径 file_path = os.path.join(folder_path, filename) # 发送请求并下载图片 response = requests.get(url) if response.status_code == 200: with open(file_path, 'wb') as f: f.write(response.content)获取网页源代码
url = "https://brickvisual.com/works/" response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser')
# 匹配大图的链接 image_regex = re.compile(r'https://brickvisual.com/wp-content/uploads/.+?.(jpg|jpeg|png)') image_urls = re.findall(image_regex, soup.prettify()) # 匹配动态加载的图片链接 javascript_regex = re.compile(r'redirect_external("(.*?)")') javascript_links = [link['onclick'] for link in soup.find_all('a', {"class": "project-image-link fancybox"})] for link in javascript_links: javascript_url = re.findall(javascript_regex, link)[0] if javascript_url.endswith('.jpg') or javascript_url.endswith('.jpeg') or javascript_url.endswith('.png'): image_urls.append(javascript_url) # 创建本地文件夹并下载图片 folder_path = r"F:\brickvisual_images" if not os.path.exists(folder_path): os.makedirs(folder_path) for image_url in image_urls: download_image(image_url, folder_path) 那么请帮我把这段脚本改一下以避免上述错误 - Assistant
可以尝试在 download_image 函数中包裹 try/except 块,并在 except 块中处理连接错误。此外,可以添加另一个 except 块来处理其他可能发生的异常。修改后的代码如下所示:
import requests from bs4 import BeautifulSoup import re import os # 创建下载图片函数 def download_image(url, folder_path): try: if not url.startswith("http://") and not url.startswith("https://"): url = "http://" + url # 获取文件名 filename = url.split("/")[-1] # 构造本地文件路径 file_path = os.path.join(folder_path, filename) # 发送请求并下载图片 response = requests.get(url) if response.status_code == 200: with open(file_path, 'wb') as f: f.write(response.content) except requests.exceptions.ConnectionError as e: print(f"Connection error: {e}") except Exception as e: print(f"Error: {e}") # 获取网页源代码 url = "https://brickvisual.com/works/" response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') # 匹配大图的链接 image_regex = re.compile(r'https://brickvisual.com/wp-content/uploads/.+?.(jpg|jpeg|png)') image_urls = re.findall(image_regex, soup.prettify()) # 匹配动态加载的图片链接 javascript_regex = re.compile(r'redirect_external("(.*?)")') javascript_links = [link['onclick'] for link in soup.find_all('a', {"class": "project-image-link fancybox"})] for link in javascript_links: javascript_url = re.findall(javascript_regex, link)[0] if javascript_url.endswith('.jpg') or javascript_url.endswith('.jpeg') or javascript_url.endswith('.png'): image_urls.append(javascript_url) # 创建本地文件夹并下载图片 folder_path = r"F:\brickvisual_images" if not os.path.exists(folder_path): os.makedirs(folder_path) for image_url in image_urls: download_image(image_url, folder_path)现在,当下载任何图片时,如果出现连接错误,则会捕获并输出相应的错误信息,使代码更加健壮。
- User
缩进很重要,你现在给我的代码没有缩进,请发给我正确的代码
- Assistant
非常抱歉!以下是正确缩进的代码:
import requests from bs4 import BeautifulSoup import re import os # 创建下载图片函数 def download_image(url, folder_path): try: if not url.startswith("http://") and not url.startswith("https://"): url = "http://" + url # 获取文件名 filename = url.split("/")[-1] # 构造本地文件路径 file_path = os.path.join(folder_path, filename) # 发送请求并下载图片 response = requests.get(url) if response.status_code == 200: with open(file_path, 'wb') as f: f.write(response.content) except requests.exceptions.ConnectionError as e: print(f"Connection error: {e}") except Exception as e: print(f"Error: {e}") # 获取网页源代码 url = "https://brickvisual.com/works/" response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') # 匹配大图的链接 image_regex = re.compile(r'https://brickvisual.com/wp-content/uploads/.+?.(jpg|jpeg|png)') image_urls = re.findall(image_regex, soup.prettify()) # 匹配动态加载的图片链接 javascript_regex = re.compile(r'redirect_external("(.*?)")') javascript_links = [link['onclick'] for link in soup.find_all('a', {"class": "project-image-link fancybox"})] for link in javascript_links: javascript_url = re.findall(javascript_regex, link)[0] if javascript_url.endswith('.jpg') or javascript_url.endswith('.jpeg') or javascript_url.endswith('.png'): image_urls.append(javascript_url) # 创建本地文件夹并下载图片 folder_path = r"F:\brickvisual_images" if not os.path.exists(folder_path): os.makedirs(folder_path) for image_url in image_urls: download_image(image_url, folder_path)再次感谢您指出我的错误。