2017-06-21 | 研究与探索 | UNLOCK

日志47150621

今天写了一段爬虫代码——其实连雏形都算不上。用的Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#codeing='utf-8'

import requests

import os

# i in 1 to 12,j in 1 to 10
def geturl1(i,j):
return ('''http://****.com/media/_10b76617/assets/girls/girl_{0:0>2}/images/image{1}n.jpg''').format(i,j)

# i in 1 to 12,j in 1 to 5
def geturl1s(i,j):
return ('''http://****.com/media/_10b76617/assets/girls/girl_{0:0>2}/images/sex-{1}.jpg''').format(i,j)

#def getfname1(i,j):
# return ('E:\\')


rootpath = 'E:\\pictures\\' # with '/' at the end

# get folder 1s
for i in range(6,13):
if not os.path.isdir(rootpath+'1s\\'+'{:0>2}'.format(i)+'\\'):
os.mkdir(rootpath+'1s\\'+'{:0>2}'.format(i)+'\\')
for j in range(1,6):
fname = rootpath+'1s\\'+'{:0>2}'.format(i)+'\\'+'{:0>2}'.format(j)+'.jpg'
print fname
fout = open(fname,'wb')
while True:
try:
pic= requests.get(geturl1s(i,j), timeout=10)
except requests.exceptions.ConnectionError:
print 'error:current picture cannot be downloaded'
continue
break
fout.write(pic.content)
fout.close()

# get folder 1
for i in range(1,13):
if not os.path.isdir(rootpath+'1\\'+'{:0>2}'.format(i)+'\\'):
os.mkdir(rootpath+'1\\'+'{:0>2}'.format(i)+'\\')
for j in range(1,11):
fname = rootpath+'1\\'+'{:0>2}'.format(i)+'\\'+'{:0>2}'.format(j)+'.jpg'
print fname
fout = open(fname,'wb')
while True:
try:
pic = requests.get(geturl1(i,j), timeout=10)
except requests.exceptions.ConnectionError:
print 'error:current picture cannot be downloaded'
continue
break
fout.write(pic.content)
fout.close()

缩进问题真可恨