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/

posted @ 2013-06-30 19:01  good90  阅读(299)  评论(0编辑  收藏  举报