google-images-download 报 Could not open URL.....的解决方法

在使用 google-images-download 过程中出现  Could not open URL. Please check your internet connection and/or ssl settings
If you are using proxy, make sure your proxy settings is configured correctly 问题的解决方法

 

在已经使用科学上网方式运行 google-images-download 但是还是出现 Could not open URL. Please check your internet connection and/or ssl settings If you are using proxy, make sure your proxy settings is configured correctly 问题 

google-images-download中部分需要修改的代码:

def _find_last(self,string,str):
        last_position=-1
        while True:
            position=string.find(str,last_position+1)
            if position==-1:
                return last_position
            last_position=position


 # Downloading entire Web Document (Raw Page Content)
    def download_page(self, url):
        version = (3, 0)
        cur_version = sys.version_info
        if cur_version >= version:  # If the Current Version of Python is 3.0 or above
            try:
                ### 这部分是我使用代理  可以去掉
                proxy_support = urllib.request.ProxyHandler({'https':'127.0.0.1:51593'})
                opener = urllib.request.build_opener(proxy_support)
                urllib.request.install_opener(opener)
                ###
                headers = {}
                headers[
                    'User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.17 Safari/537.36"
                req = urllib.request.Request(url, headers=headers)
                resp = urllib.request.urlopen(req)
                respData = str(resp.read())
                respDatas = self._extract_data_pack(respData)
                respDatas = respDatas[:self._find_last(respDatas,",")]
                return self._image_objects_from_pack(respDatas), self.get_all_tabs(respData)
            except Exception as e:
                print("Could not open URL. Please check your internet connection and/or ssl settings \n"
                      "If you are using proxy, make sure your proxy settings is configured correctly")
                sys.exit()
        else:  # If the Current Version of Python is 2.x
            try:
                headers = {}
                headers[
                    'User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
                req = urllib2.Request(url, headers=headers)
                try:
                    response = urllib2.urlopen(req)
                except URLError:  # Handling SSL certificate failed
                    context = ssl._create_unverified_context()
                    response = urlopen(req, context=context)
                page = response.read()
                return self._image_objects_from_pack(self._extract_data_pack(page)), self.get_all_tabs(page)
            except:
                print("Could not open URL. Please check your internet connection and/or ssl settings \n"
                      "If you are using proxy, make sure your proxy settings is configured correctly")
                sys.exit()
                return "Page Not found"

 

 

文件资源审核中。。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值