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/

  • 相关阅读:
    Mysql 数据库高级
    Mysql 数据库
    并发编程
    网络编程
    1113
    1112
    P相遇游戏
    中位数
    PETS
    打暴力程序的正确做法
  • 原文地址:https://www.cnblogs.com/good90/p/3163756.html
Copyright © 2011-2022 走看看