Skip to content

Commit 28f9331

Browse files
author
yincongxian
committed
Add the solution to 0013.
1 parent c469927 commit 28f9331

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

renzongxian/0013/0013.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
2+
# Author:renzongxian
3+
# Date:2014-12-24
4+
# Python 3.4
5+
6+
"""
7+
8+
第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)
9+
10+
"""
11+
12+
import urllib.request
13+
import re
14+
import os
15+
16+
17+
def fetch_pictures(url):
18+
html_content = urllib.request.urlopen(url).read()
19+
r = re.compile('<img pic_type="0" class="BDE_Image" src="(.*?)"')
20+
picture_url_list = r.findall(html_content.decode('utf-8'))
21+
22+
os.mkdir('pictures')
23+
os.chdir(os.path.join(os.getcwd(), 'pictures'))
24+
for i in range(len(picture_url_list)):
25+
picture_name = str(i) + '.jpg'
26+
try:
27+
urllib.request.urlretrieve(picture_url_list[i], picture_name)
28+
print("Success to download " + picture_url_list[i])
29+
except:
30+
print("Fail to download " + picture_url_list[i])
31+
32+
if __name__ == '__main__':
33+
fetch_pictures("http://tieba.baidu.com/p/2166231880")

0 commit comments

Comments
 (0)