您的位置:首页 > 编程语言 > Python开发

python 学习笔记

2016-07-14 18:16 405 查看
新手刚接触python 做了下面的例子,这个例子的功能下载指定网页上的图片:

import urllib.request
import re
import threading
from time import sleep,ctime
from html import parser

#connect to a URL
website = urllib.request.urlopen("http://sc.chinaz.com/tupian/taikongtupian_3.html")
#read html code
html = website.read().decode()
#use re.findall to get all the links
links = re.findall(r'(http:[^\s]*?(jpg|png|gif|JPG|PNG|GIF))', html)
print(links)
print ("网页抓取成功,开始下载图片:")

def downjpg( filepath,FileName ="default.jpg" ):
try:
print(str(filepath))
web = urllib.request.urlopen(filepath)
print("访问网络文件"+"\n")
jpg = web.read()
DstDir="G:\\image\\"
print("保存文件"+DstDir+FileName+"\n")
try:
File = open( DstDir+FileName,"wb" )
File.write( jpg)
File.close()
return
except IOError:
print("error\n")
return
except Exception:
print("error\n")
return

def upload(url,name):
path = r"G:\\image\\"+name

data = urllib.request.urlopen(url).read()
print(url)
f = open(path,"wb")
print(path)
f.write(data)
print(data)
f.close()

i=150
it = iter(links)    # 创建迭代器对象
for x in it:
i+=1
name=str(i)+".jpg"
upload(x[0],name)
#downjpg(x[0],name)
#print(re.findall('"http:.*"', x)[0]+name)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: