通过eventlet抓取和保存文件时出现问题

| 我可以使用Evenlet从网站上删除img,但无法将其保存到国内目录中。 以下是代码。有人熟悉Tasklet模型中的I / O操作吗?谢谢
import pyquery
import eventlet
from eventlet.green import urllib2

#fetch img urls............ works fine

print \"loading page...\"
html=urllib2.urlopen(\"http://www.meinv86.com/meinv/yuanchuangmeinvzipai/\").read()
print \"Parsing urls...\"
d=pyquery.PyQuery(html)
count=0
urls=[]
url=\'\'
for i in d(\'img\'):
 count=count+1
 print i.attrib[\"src\"]
 urls.append(i.attrib[\"src\"])


def fetch(url):
 try:
  print \"start feteching %s\" %(url)
  urlfile = urllib2.urlopen(url)
  size=int(urlfile.headers[\'content-length\'])
  print \'downloading %s, total file size: %d\' %(url,size)
  data = urlfile.read()
  print \'download complete - %s\' %(url)

##########################################
#file save just won\'t work

  f=open(\"/head2/\"+url+\".jpg\",\"wb\")
  f.write(body)  
  f.close()
  print \"file saved\"
##########################################  

  return data

 except:
  print \"fail to download...\"




pool = eventlet.GreenPool()

for body in pool.imap(fetch, urls):
  print \"done\"
    
已邀请:
确保
url
适合作为文件名,例如:
import hashlib
import os

def url2filename(url, ext=\'\'):
    return hashlib.md5(url).hexdigest() + ext # anything that removes \'\\/\'

# ...
with open(os.path.join(\"/head2\", url2filename(url, \'.jpg\')), \'wb\') as f:
     f.write(body)
print \"file saved\"
注意:您可能不想将文件写入顶级目录,例如
\'/head2\'
。 您也可以考虑
urllib.urlretrieve()
。     

要回复问题请先登录注册