python 学习笔记

新手刚接触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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值