A simple example of a Python crawler

Source: Internet
Author: User

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:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.