1. Pycharm设置

2. urllib下载数据配置
from urllib.error import URLError from urllib.request import ProxyHandler, build_opener 参数是字典,键名是协议类型,健值是代理 proxy_handler = ProxyHandler({
'http': "http://127.0.0.1:12333", 'https': "http://127.0.0.1:12333" }) # Opener已经设置好代理了 opener = build_opener(proxy_handler) try: response = opener.open('http://httpbin.org/get') # 运行结果是一个JSON print(response.read().decode('utf-8')) except URLError as e: print(e.reason)
Torch.geometirc dowload.py数据下载实例:
def download_url(url: str, folder: str, log: bool = True): r"""Downloads the content of an URL to a specific folder. Args: url (string): The url. folder (string): The folder. log (bool, optional): If :obj:`False`, will not print anything to the console. (default: :obj:`True`) """ filename = url.rpartition('/')[2] filename = filename if filename[0] == '?' else filename.split('?')[0] path = osp.join(folder, filename) if osp.exists(path): # pragma: no cover if log: print(f'Using existing file {filename}', file=sys.stderr) return path if log: print(f'Downloading {url}', file=sys.stderr) makedirs(folder) context = ssl._create_unverified_context() from urllib.request import ProxyHandler, build_opener proxy_handler = ProxyHandler({
'http': "http://127.0.0.1:12333", 'https': "http://127.0.0.1:12333" })# Opener已经设置好代理了 opener = build_opener(proxy_handler) data = opener.open(url) # data = urllib.request.urlopen(url, context=context) with open(path, 'wb') as f: f.write(data.read()) return path
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/173506.html原文链接:https://javaforall.net
