zoukankan      html  css  js  c++  java
  • python学习--抓取一个网页上图片

     1 #!/bin/python
     2 # download_pic.py
     3 # download picture
     4 import os
     5 import sys
     6 from urllib.request import urlopen
     7 
     8 url = sys.argv[1]
     9 page = urlopen(url)
    10 #需要指定编码格式
    11 html = str(page.read(),encoding='utf8')
    12 page.close()
    13 
    14 #create directory
    15 if os.path.exists("./picture") == False:
    16     os.makedirs('./picture')
    17     os.chdir('./picture')
    18 
    19 head = html.find('<img')
    20 while head != -1:
    21     html = html[head:]
    22     http_idx = html.find('http')
    23     if http_idx == -1:
    24         break
    25     html = html[http_idx:]
    26     tail = html.find('"')
    27     url = html[:tail]
    28 
    29     print("url:", url)
    30     cmd = "wget {0}".format(url)
    31     os.system(cmd)
    32     print ("cmd:", cmd)
    33     #html = page2[tail:]
    34     head = html.find('<img')
    View Code

     用法: python download_pic.py http://image.baidu.com/

  • 相关阅读:
    主函数main
    static关键字
    this关键字
    构造函数
    封装
    匿名对象
    java基础积累
    JAVA相关知识复习
    ORACLE数据库表空间查询
    两个日期的时间差
  • 原文地址:https://www.cnblogs.com/good90/p/3163756.html
Copyright © 2011-2022 走看看