本文整理汇总了Python中urllib.error.ContentTooShortError方法的典型用法代码示例。如果您正苦于以下问题:Python error.ContentTooShortError方法的具体用法?Python error.ContentTooShortError怎么用?Python error.ContentTooShortError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.error
的用法示例。
在下文中一共展示了error.ContentTooShortError方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_voice
# 需要导入模块: from urllib import error [as 别名]
# 或者: from urllib.error import ContentTooShortError [as 别名]
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
示例2: download
# 需要导入模块: from urllib import error [as 别名]
# 或者: from urllib.error import ContentTooShortError [as 别名]
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
示例3: cq_get_image
# 需要导入模块: from urllib import error [as 别名]
# 或者: from urllib.error import ContentTooShortError [as 别名]
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
示例4: download_file_from_qzone
# 需要导入模块: from urllib import error [as 别名]
# 或者: from urllib.error import ContentTooShortError [as 别名]
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
'''
示例5: download_user_avatar
# 需要导入模块: from urllib import error [as 别名]
# 或者: from urllib.error import ContentTooShortError [as 别名]
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
示例6: download_group_avatar
# 需要导入模块: from urllib import error [as 别名]
# 或者: from urllib.error import ContentTooShortError [as 别名]
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
示例7: get_zhaopin_html
# 需要导入模块: from urllib import error [as 别名]
# 或者: from urllib.error import ContentTooShortError [as 别名]
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选择器