#! /usr/bin/python
# -*- coding:utf-8 -*-
import time
import re
import http
import tools
import urllib
import urllib2
import os
from socket import error as SocketError
class PICS:
def __init__(self):
self.URL = 'http://super-search.u.qiniudn.com/'
self.params = '_magazine_'
# 开启网络请求
def requestOpener(self, url):
try:
opener = urllib2.build_opener(urllib2.HTTPHandler())
opener.addheaders = [('User_Agent',
'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1')]
response = opener.open(url, timeout=20)
return 'isExits'
except urllib2.HTTPError, e:
print e.code
return ''
except urllib2.URLError, e:
print e.reason
return ''
except SocketError, e:
# print 'SocketError: ' + str(e.errno)
return ''
def writeFile(self, picUrl):
try:
with open('./file/pic.txt', 'ab+') as f:
f.write(picUrl+'\n')
except Exception, e:
print 'Exception: ' + str(e)
def addPicUrl(self, sort):
fromId = 1
while True:
url = self.URL + sort + self.params + str(fromId) +'.jpg'
pic = self.requestOpener(url)
if pic == '':
break
else:
self.writeFile(url)
fromId += 1
print fromId
print url
def readFile(self):
file = open('./file/sort.txt')
for line in file:
self.addPicUrl(line.strip('\n'))
# print self.URL + line.strip('\n') + self.params + '1.jpg'
if __name__ == '__main__':
t1 = time.time()
PICS().readFile()
t2 = time.time() - t1
print '总共用时:' + str(t2)