Python urllib.error.ContentTooShortError() Examples
The following are 7
code examples of urllib.error.ContentTooShortError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
urllib.error
, or try the search function
.
Example #1
Source File: Utils.py From efb-qq-slave with GNU General Public License v3.0 | 6 votes |
def download_voice(filename: str, api_root: str, access_token: str): file = tempfile.NamedTemporaryFile() url = '{url}/data/record/{file}'.format(url=api_root, file=filename) try: opener = urllib.request.build_opener() opener.addheaders = [("Authorization", "Bearer {at}".format(at=access_token))] urllib.request.install_opener(opener) urllib.request.urlretrieve(url, file.name) except (URLError, HTTPError, ContentTooShortError) as e: logging.getLogger(__name__).warning("Error occurs when downloading files: " + str(e)) return _("Error occurs when downloading files: ") + str(e) if file.seek(0, 2) <= 0: raise EOFError('File downloaded is Empty') file.seek(0) return file
Example #2
Source File: Utils.py From efb-qq-slave with GNU General Public License v3.0 | 5 votes |
def cq_get_image(image_link: str) -> tempfile: # Download image from QQ file = tempfile.NamedTemporaryFile() try: urllib.request.urlretrieve(image_link, file.name) except (URLError, HTTPError, ContentTooShortError) as e: logging.getLogger(__name__).warning('Image download failed.') logging.getLogger(__name__).warning(str(e)) return None else: if file.seek(0, 2) <= 0: raise EOFError('File downloaded is Empty') file.seek(0) return file
Example #3
Source File: Utils.py From efb-qq-slave with GNU General Public License v3.0 | 5 votes |
def download_file_from_qzone(cookie: str, csrf_token: str, uin, group_id, file_id, filename, file_size): cookie_arr = param_spliter(cookie) url = "http://qun.qzone.qq.com/cgi-bin/group_share_get_downurl?uin=" + str(uin) + "&pa=/104/" + \ str(file_id) + "&groupid=" + str(group_id) + "&bussinessid=0&charset=utf-8&g_tk=" + str(csrf_token) + "&r=888" ret = requests.get(url, cookies=cookie_arr) data = json.loads(ret.text.split("(")[1].split(")")[0])['data'] cookie += "; FTN5K=" + str(data['cookie']) download_url = data['url'] download_url += "/" + quote(filename) if file_size >= 50*1024*1024: # File size is bigger than 50MiB return _("File is too big to be downloaded") file = tempfile.NamedTemporaryFile() try: opener = urllib.request.build_opener() opener.addheaders.append(('Cookie', cookie)) urllib.request.install_opener(opener) urllib.request.urlretrieve(download_url, file.name) except (URLError, HTTPError, ContentTooShortError) as e: logging.getLogger(__name__).warning("Error occurs when downloading files: " + str(e)) return _("Error occurs when downloading files: ") + str(e) else: if file.seek(0, 2) <= 0: raise EOFError('File downloaded is Empty') file.seek(0) return file ''' try: opener = urllib.request.build_opener() opener.addheaders.append(('Cookie', cookie)) with opener.open(download_url) as response, tempfile.NamedTemporaryFile() as f: shutil.copyfileobj(response, f) if f.seek(0, 2) <= 0: raise EOFError('File downloaded is Empty') f.seek(0) return f except Exception as e: logging.getLogger(__name__).warning("Error occurs when downloading files" + str(e)) return url '''
Example #4
Source File: Utils.py From efb-qq-slave with GNU General Public License v3.0 | 5 votes |
def download_user_avatar(uid: str): file = tempfile.NamedTemporaryFile() url = "https://q1.qlogo.cn/g?b=qq&nk={}&s=0".format(uid) try: opener = urllib.request.build_opener() urllib.request.install_opener(opener) urllib.request.urlretrieve(url, file.name) except (URLError, HTTPError, ContentTooShortError) as e: logging.getLogger(__name__).warning("Error occurs when downloading files: " + str(e)) return _("Error occurs when downloading files: ") + str(e) if file.seek(0, 2) <= 0: raise EOFError('File downloaded is Empty') file.seek(0) return file
Example #5
Source File: Utils.py From efb-qq-slave with GNU General Public License v3.0 | 5 votes |
def download_group_avatar(uid: str): file = tempfile.NamedTemporaryFile() url = "https://p.qlogo.cn/gh/{}/{}/".format(uid, uid) try: opener = urllib.request.build_opener() urllib.request.install_opener(opener) urllib.request.urlretrieve(url, file.name) except (URLError, HTTPError, ContentTooShortError) as e: logging.getLogger(__name__).warning("Error occurs when downloading files: " + str(e)) return _("Error occurs when downloading files: ") + str(e) if file.seek(0, 2) <= 0: raise EOFError('File downloaded is Empty') file.seek(0) return file
Example #6
Source File: dl_bt_dukascopy.py From FX-BT-Scripts with MIT License | 5 votes |
def download(self): print("Downloading %s into: %s..." % (self.url, self.path)) if os.path.isfile(self.path): print("File (%s) exists, so skipping." % self.path) return True else: if not os.path.exists(os.path.dirname(self.path)): os.makedirs(os.path.dirname(self.path)) i = 1 while i <= 5: try: urllib.request.urlretrieve(self.url, filename=self.path) break except HTTPError as err: print("Error: %s, reason: %s. Retrying (%i).." % (err.code, err.reason, i)) i += 1 except IOError as err: print("Error: %s, reason: %s. Retrying (%i).." % (err.errno, err.strerror, i)) i += 1 except socket.timeout as err: print("Network error: %s. Retrying (%i).." % (err.strerror, i)) i += 1 except socket.error as err: print("Network error: %s. Retrying (%i).." % (err.strerror, i)) i += 1 except ContentTooShortError as err: print("Error: The downloaded data is less than the expected amount, so skipping.") i += 1 if i == 5: return False return True
Example #7
Source File: main.py From python_spider_jobs with Apache License 2.0 | 4 votes |
def get_zhaopin_html(jobarea_name, job_type): url_temp = "http://sou.zhaopin.com/jobs/searchresult.ashx?jl={jobarea_name}&kw={job_type}&sm=0&p=1&source=1" url = url_temp.format(jobarea_name=urllib.request.quote(jobarea_name),job_type=urllib.request.quote(job_type)) headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36' } logger.warning("-------->15-->02") try: time.sleep(2) req = urllib.request.Request(url, None, headers) response = urlopen(req) # response = urllib.request.urlopen(url) # 打开网址 except HTTPError as e1: print("The (www.python.org)server couldn't fulfill the request.") logger.error('-------->15-->02->HTTPError-> %s' % ( e1.msg)) except URLError as e2: print('We failed to reach a server.') logger.error('-------->15-->03->URLError->%s' % (e2.msg)) except ContentTooShortError as e3: print('Reason: ', e3.reason) logger.error('-------->15-->04->ContentTooShortError-> %s' % (e3.msg)) else: html = "" logger.warning("-------->15-->05") try: if response: logger.warning("-------->15-->05-->01a") html = response.read() # 读取源代码并转为unicode logger.warning("-------->15-->05-->01b") else: logger.warning("-------->15-->05-->02") except Exception as e4: logger.error('-------->15-->06->URLError->%s' % (str(e4))) finally: logger.warning("-------->15-->07-->finally") response.close() logger.warning("-------->15-->08") if html: return html.decode('UTF-8') else: logger.error("-------->15-->09-> html is None") return "" # 解析 智联招聘:zhaopin.com 页面的, css选择器