闲来没事写了个python爬爬

本文通过一个具体的爬虫实例,展示了如何使用Python批量下载指定网站的图片及相应资源包。该爬虫能够自动解析网页源码,提取图片链接和下载链接,并将资源按类别保存到本地文件夹中。
import urllib2,urllib
import os,re

if __name__ == '__main__':
    
	pathw = os.getcwd()

	for i in range(5, 5365 + 1):
		try:
			response = urllib2.urlopen('http://www.cssmoban.com/cssthemes/'+str(i)+'.shtml') 
			html = response.read()

			imagetitleregion = r'<div class="large-Imgs">\r\n								<img src="(.+?)" alt="(.+?)">'
			print imagetitleregion
			#提取图片和标题
			m = re.compile(imagetitleregion).findall(html,re.S)
			
			imageurl = m[0][0].decode("utf-8")
			title = m[0][1].decode("utf-8")
			path = pathw+'\\'+title+'_'+str(i)
			#print imageurl
			if not(os.path.isdir(path)):
				os.mkdir(path)

			if imageurl != u'佚名':
				urllib.urlretrieve('http://www.cssmoban.com'+imageurl, path+'\\'+str(i)+'.jpg')

			#提取文件路径
			downregion = '<a href="(.+?)" target="_blank" class="button btn-down" title="免费下载"><i class="icon-down icon-white"></i><i class="icon-white icon-down-transiton"></i>免费下载</a>'
			m = re.compile(downregion).findall(html)

			urllib.urlretrieve(m[0], path+'\\'+str(i)+'.rar')
		except:  
			print '爬页面数据失败'

代码贴出来了

转载于:https://my.oschina.net/yh211/blog/301458

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值