pycode0013

第 0013 题

用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 🙂


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests
import re
from pathlib import Path
if __name__ == '__main__':
response = requests.get('http://tieba.baidu.com/p/2166231880')
content = str(response.content)
# 图片存放路径
path = Path('resource/imgs/')
if not path.exists():
path.mkdir()
# 正则匹配图片链接
p = r'<img.*?class="BDE_Image" src="(.*?)".*?>'
result = re.findall(p, content)
# 获取图片并写入
for i in result:
resp = requests.get(i)
filename = re.search(r'/([0-9a-f]+.w+)', i).group(1)
with open('resource/imgs/'+filename, 'wb') as f2:
f2.write(resp.content)