Skip to content

Commit dd13888

Browse files
committed
download
1 parent 2bddecf commit dd13888

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

studyLib/spider/download.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# encoding:UTF-8
2+
from urllib import request, parse
3+
import re, urllib
4+
5+
baseurl = 'https://www.baidu.com/img/wanshengdoodle_677234cad70a5974a64e4665c6485c71.gif'
6+
headers = {
7+
'Connection': 'Keep-Alive',
8+
'Accept': 'application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*',
9+
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
10+
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)'
11+
}
12+
13+
try:
14+
req = urllib.request.Request(baseurl, headers=headers)
15+
urlop = urllib.request.urlopen(req, timeout=5)
16+
except Exception as err:
17+
print('1', err)
18+
19+
# 避免程序异常中止, 用try..catch处理异常
20+
filename = "c:\\shaohua.huo\\aaa.gif"
21+
try:
22+
data = urlop.read()
23+
with open(filename, mode='wb') as save_file:
24+
save_file.write(data)
25+
except Exception as err:
26+
print('2', err)

0 commit comments

Comments
 (0)