首先是conf.py文件,主要是配置一些会用到的参数
api_url=u'https://pcs.baidu.com/rest/2.0/pcs/file' method=u'upload' path=u'/apps/pcstest_oauth/pic2' access_token=u'23.1c19e723c137c6f137ff1f625fa0aaa4.2592000.1414472077.328831-238347'
#encoding:utf-8 import urllib,re,time from conf import api_url,method,access_token,path import requests#该模块用来完成上传文件 num=0 def getURL(n):#解析页面所有妹子图片地址链接,返回包含链接的一个列表 url=urllib.urlopen('http://www.dbmeizi.com/category/2?p=%d'%n) data=url.read() r=re.compile(r'http://pic.dbmeizi.com/npics/[a-z0-9-]{3}/[a-z0-9-]{3}/s_p[0-9]{8}.jpg')#用于匹配的正则表达式 pic=r.findall(data) return pic def upload(data):#模拟一个post请求完成图片上传 files = {'file': data2} print path+'/meizhi%d.jpg'%num params={ 'method':method, 'path':path+'/meizhi%d.jpg'%num, 'access_token':access_token, } url='%s?%s' % (api_url, urllib.urlencode(params)) print url response=requests.post(url,files=files) print response.text for page in range(1,300): girl=getURL(page)# print girl for g in girl: url2=urllib.urlopen(g) data2=url2.read()#将图片下载到内存 upload(data2)#upload to pcs print num num=num+1 time.sleep(0.1) #每100ms搬运一张 print 'end'
原文地址:http://blog.csdn.net/yukjin/article/details/41866367