python批量下载色影无忌和蜂鸟的图片 爬虫小应用

有些冗余信息,因为之前测试正则表达式,所以没有把它们给移走,不过不影响使用。
# -*- coding:utf-8 -*-
import re,urllib,sys,os,time


def getAllUrl():
	entry=sys.argv[1]
	#try:
	getPage=urllib.urlopen(entry).read()
	#except:
	#	print "Error"
	
	pattern=re.compile(r'<a href="(.+?)".+?>')
	web_site_pattern=re.compile(r'(http:.+?)')
	all_url = pattern.findall(getPage)
	for url in all_url:
		if web_site_pattern.match(url):
			print url
		#print url
		
	print "done"

def download_pic():
	url=sys.argv[1];
	#local_path="C:/Tools/source/"
	connection=urllib.urlopen(url)
	data=connection.read()
	print "Waiting to get data"
	time.sleep(3)
	connection.close()
	
	#analyze 
	#p=re.compile(r'img width="\d+".+src="(.+)".+')
	download_pic_pattern=re.compile(r'<img src="(.+?\.jpg)".+?/>')
	#p10=re.compile(r'(.+)\.jpg')
	all_url=download_pic_pattern.findall(data)
	#print all_url
	i=1
	directory="C:/Tools/source"
	name_pattern=re.compile(r'/(\w+?\.jpg)')
	if not os.path.exists(directory):
		os.mkdir(directory)

	for urls in all_url:
		print urls
		#print "working"
		
		
		#print local_path	
		i=i+1
		name=name_pattern.findall(urls)
		print name[0]
		local_path="C:/Tools/source/%s" % name[0] 
		jpeg_connection=urllib.urlopen(urls)
		
		jpeg=jpeg_connection.read()
		time.sleep(1)
		print "waiting"
		f=file(local_path,"wb")
		f.write(jpeg)
		
		f.close()
		jpeg_connection.close()
		#i=i+1
		
	#f=file(local_path,"wb")
	#f.write(data)
	#f.close()
	print("Done")
	
def download_pic_2():
	url=sys.argv[1];
	local_path="C:/Tools/a.jpg"
	data=urllib.urlretrieve(url,local_path)
	print("Done")


def regulation():
	str1="abc123*GBK1024abc*defb1kc12*addd"
	
	p1=re.compile(r'abc')
	print p1.findall(str1)

	p2=re.compile(r'a.c')
	print p2.findall(str1)

	p3=re.compile(r'abc\*')
	print p3.findall(str1)
	
	p4=re.compile(r'[abc]12')
	print p4.findall(str1)

	p5=re.compile(r'\d\*')
	print p5.findall(str1)

	p6=re.compile(r'a[^\d]')
	print p6.findall(str1)
	
	p7=re.compile(r'a[^\d]*')
	print p7.findall(str1)

	p8=re.compile(r'[a-zA-Z]+(\d+)')
	print p8.findall(str1)

	str2="dadfae ef <img atl=\"500\" src=\"www.qq.com/1.jpg\" width=\"700\"> asdfe aa<ima"

	p9=re.compile(r'<img .+ src="(.+)" .+>')
	urls=p9.findall(str2)
	#print
	print urls
	for url in urls:
		print url

	
	
	
	
if __name__ =="__main__":
	#main()
	#download_pic_2()
	#regulation()
	download_pic()
	#getAllUrl()


#######后续

后面修改了代码,使用beautifulsoup,可以更大范围的下载图片

代码如下: http://www.30daydo.com/article/56


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值