【发布时间】:2014-03-07 16:50:43
【问题描述】:
我有一个 python 脚本正在运行,它基本上通过 http 请求 1000 个 url 并记录它们的响应。这是下载url页面的函数。
def downld_url(url, output):
print "Entered Downld_url and scraping the pdf/doc/docx file now..."
global error
try:
# determine all extensions we should account for
f = urllib2.urlopen(url)
data = f.read()
dlfn = urlsplit(url).path.split('.')[-1]
print "The extension of the file is: " + str(dlfn)
dwnladfn = ImageDestinationPath + "/" + output + "." + dlfn
with open(dwnladfn, "wb") as code:
code.write(data)
code.close()
_Save_image_to_s3(output+"."+dlfn, ImageDestinationPath + "/" +output +
"." + dlfn)
print dlfn + " file saved to S3"
os.remove(ImageDestinationPath + "/" +output + "." + dlfn)
print dlfn + "file removed from local folder"
update_database(output,output+"."+dlfn, None)
return
except Exception as e:
error = "download error: " + str(e)
print "Error in downloading file: " + error
return
现在,管道中的 100-200 个 url 可以顺利运行,但之后响应开始变得非常缓慢,最终响应超时。我是,猜测这是因为请求过载。有没有一些有效的方法可以在不重载请求的情况下做到这一点?
【问题讨论】:
-
注意:这些网址大多是下载的 .png 和 .pdf 文件。
-
不相关:使用
urlparse解析url和os.path,posixpath操作路径 -
哪些请求变慢了:是
urlopen(),还是_Save_image_to_s3,还是update_database(),还是别的什么?
标签: python http python-2.7 python-3.x https