-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_fetch_bing_img_everyday.py
More file actions
executable file
·62 lines (50 loc) · 1.71 KB
/
Copy pathpy_fetch_bing_img_everyday.py
File metadata and controls
executable file
·62 lines (50 loc) · 1.71 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re, socket, select, time, urlparse, os
def _req(url):
url_arr = urlparse.urlparse(url)
r = 'GET '+(url_arr[2] if url_arr[2] != '' else '/')+' HTTP/1.1\r\nHost: '+url_arr[1]+'\r\n\r\n'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((url_arr[1], 80))
s.send(r)
data = ''
e_count = 0
while True:
e_count += 1
recvs, _, error = select.select([s], [], [], 0.1) #can down to 0.03, but lose data sometimes
if error:
break
if recvs:
print 'start recv..'
for recv in recvs:
d = recv.recv(9000)
if (d):
print 'data length: ', len(d)
data = data + d
e_count = 0
if e_count == 5:
break
print e_count
s.close()
if data == '':
data = _req(url)
return data
if __name__ == '__main__':
data = _req('http://cn.bing.com')
reg = re.compile("g_img=\{url:'([^']+)'")
m = reg.search(data)
if m is not None:
print 'img url: ', m.group(1)
img_data = _req(m.group(1))
img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bing_images')
if not os.path.isdir(img_path):
os.mkdir(img_path)
f = open(img_path+'/'+time.strftime('%Y-%m-%d')+'.'+m.group(1).split('.')[-1], 'wb')
img_arr = img_data.split('\r\n\r\n')
img_arr.pop(0)
f.write(''.join(img_arr))
f.close()
print 'fetch img success.'
e = raw_input('Press Enter to exit..')
else:
print 'img not found.'