起因
因为最近在看爬虫,所以写了一个简单的小程序来爬一些图片,练习一下库的使用。
源码
#coding=utf-8
'''
糗事百科成人版爬虫
'''
import urllib
import re
head = 0
n = 1
while n < 10:
page = n
url = 'http://www.qiushimm.com/page/' + str(page)
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'src="(.+?\.jpg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = head
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg' % x)
x+=1
html = getHtml(url)
print getImg(html)
n = n+1
head = head + 10