After studying python recently, after learning grammar and doing a simple reptile out of interest, now paste the code as follows:
#!/usr/bin/python
#conding =utf-8
Import re
Import Urllib
Import Sys
Import OS
#获取参数, write to the data directory where the directory is located
Times = sys.argv[1]
Print times
def gethtml (URL):
page = Urllib.urlopen (URL)
html = Page.read ()
return HTML
#验证当前url是否可以访问
def isable2visit (URL):
StatusCode =urllib.urlopen (URL). GetCode ()
if (StatusCode = 200):
Return True
Else
Return False
#创建目录
def createdir (dir):
If not os.path.exists (dir):
Os.makedirs (dir)
Print "Success to create file" + Dir
Return dir
#获取图片保存到本地
def getimg (html,x,times):
Reg = R ' src= ' (. +?\.jpg) "Pic_ext"
Imgre = Re.compile (reg)
Imglist = Re.findall (imgre,html)
For Imgurl in Imglist:
Local = dir + os.sep + str (x) + '. jpg '
Urllib.urlretrieve (imgurl,local)
X+=1
return x
URLs = raw_input ("Enter the PreFix of the URL:")
If Len (urls) = 0:
URLs = "http://tieba.baidu.com/p/41254316"
Print URLs
x = 0
Storedir = "/home/liyong/python/spider/data/" +str (Times)
dir = Createdir (Storedir)
For I in range (100):
url = urls + str (i)
print "Done%.2f%%"% ((float (i)/100) *100)
if (isable2visit (URL)):
x = HTML = getimg (gethtml (URL), X,dir)
print "Done 100%"
The crawl results are as follows: