小爬静态页面图片

关于爬虫协议:

有的网站下是有文件:robots.txt

比如:

豆瓣

优快云

以慕课网http://www.imooc.com/首页为例

源代码:

#coding:utf-8
class Outputer:
	def __init__(self,directory):
		self.directory=directory
	def output(self,file,content):
		try:
			f=open(self.directory+file,'wb')
			f.write(content)
		except Exception as err:
			print(err)
		finally:
			f.close()
			print(file+"has writed!")


class SpiderMan:
	def __init__(self,url,directory):
		self.url=url
		self.outputer=Outputer(directory)
	def crawl(self):
		resp=request.urlopen(self.url)
		buff=resp.read()
		pattern=r'http:.+\.jpg'
		jpgurls=re.findall(pattern,buff.decode('utf-8'),flags=0)
		count=1
		for jurl in jpgurls:
			try:
				content=request.urlopen(jurl).read()
				print(count)
				count=count+1
				self.outputer.output('imooc_'+str(count)+'.jpg',content)
			except UnicodeEncodeError  as err:
				print(err)

if __name__ == '__main__':
	from  urllib import request
	import re
	#http://www.imooc.com/
	url='http://www.imooc.com/'
	spider=SpiderMan(url,'./spider_imooc/')
	spider.crawl()

运行图:

  

转载于:https://www.cnblogs.com/jasonhaven/p/7367421.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值