爬图片

1 import urllib.request as ur
2 response = ur.urlopen('http://img.zcool.cn/community/0142135541fe180000019ae9b8cf86.jpg@1280w_1l_2o_100sh.png')
3 cat_img = response.read()
4 with open('kang.png','wb') as f:
5     f.write(cat_img)

简单爬图片,功能需增强

大佬代码:

 1 import requests
 2 from bs4 import BeautifulSoup
 3 circle = requests.get('http://travel.quanjing.com/tag/12975/%E9%A9%AC%E5%B0%94%E4%BB%A3%E5%A4%AB')
 4 
 5 # 将获取的图片地址依次放入count中
 6 count = []
 7 # 将获取的网页内容放入BeautifulSoup
 8 soup = BeautifulSoup(circle.text, 'lxml')
 9 # 根据谷歌SelectGadGet这个插件,获取html标签,比如获取:#gallery-list
10 for item in soup.select('#gallery-list'):
11     # 用bs4中的find_all获取 #gallery-list 中是否存在 img这个标签
12     for img in item.find_all('img'):
13         print('img', img)
14         # m 是 img标签中存在的属性
15         img_path = img.get('m')
16         count.append(img_path)
17 # 用enumerate依次取出count中的图片地址 放入v中
18 for i,v in enumerate(count):
19     # 将获取的v值再次放入request中进行与网站相应
20     image = requests.get(v)
21     # 存取图片过程中,出现不能存储 int 类型,故而,我们对他进行类型转换 str()。w:读写方式打开,b:二进制进行读写。图片一般用到的都是二进制。
22     with open('D:\\img'+str(i)+'.jpg', 'wb') as file:
23         # content:图片转换成二进制,进行保存。
24         file.write(image.content)
25     print(i)
View Code

 

posted @ 2018-02-28 18:46  zzuli风尘  阅读(166)  评论(0编辑  收藏  举报