Skip to content

Commit ee93fa7

Browse files
committed
Merge pull request Show-Me-the-Code#48 from JiYouMCC/master
Add 0013
2 parents f066f7a + fdbb5a9 commit ee93fa7

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

JiYouMCC/0013/0013.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 第 0013 题: 用 Python 写一个爬图片的程序,爬 [这个链接里](http://tieba.baidu.com/p/2166231880)的日本妹子图片 :-) #
2+
3+
----
4+
5+
其实我是我为了爬我自己一个[老博客](http://ycool.com/post/ae3u4zu)上面的图片才做这题的。。
6+
7+
我很懒惰没用啥爬虫架构请无视..
8+
9+
百度贴吧上面格式绝对没有我老博客上那么老实,就将就下吧

JiYouMCC/0013/0013.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import urllib2
2+
import re
3+
import os
4+
import uuid
5+
6+
7+
def get_images(html_url='http://ycool.com/post/ae3u4zu',
8+
folder_name='jiyou_blog_PingLiangRoad',
9+
extensions=['gif', 'jpg', 'png']):
10+
request_html = urllib2.Request(html_url)
11+
try:
12+
response = urllib2.urlopen(request_html)
13+
html = response.read()
14+
r1 = r'<img.+src=\".+?\"'
15+
r2 = r'<img.+src=\"(.+?)\"'
16+
results = []
17+
imgs = []
18+
p = re.compile(r1)
19+
for m in p.finditer(html):
20+
results.append(m.group())
21+
for result in results:
22+
compile_result = re.compile(r2)
23+
imgs.append(compile_result.sub(r'\1', result))
24+
if not os.path.exists(folder_name):
25+
os.makedirs(folder_name)
26+
for img in imgs:
27+
filename = str(uuid.uuid1())
28+
ex = ''
29+
for extension in extensions:
30+
if '.%s' % extension in img:
31+
ex = '.%s' % extension
32+
if ex is '':
33+
continue
34+
filename += ex
35+
file_request = urllib2.Request(img)
36+
try:
37+
file_response = urllib2.urlopen(file_request)
38+
output = open(os.path.join(folder_name, filename), 'w+')
39+
output.write(file_response.read())
40+
print 'Image save at %s' % output.name
41+
output.close()
42+
except Exception, ex:
43+
print ex
44+
except Exception, e:
45+
print e
46+
47+
get_images()
48+
# japanese girls
49+
get_images(html_url='http://tieba.baidu.com/p/2166231880', folder_name='jp')

0 commit comments

Comments
 (0)