-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgpx-crawler.py
71 lines (60 loc) · 2.02 KB
/
gpx-crawler.py
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
63
64
65
66
67
68
69
70
71
import codecs;
import urllib
import re;
import json;
from lxml import etree
folder = u'/var/user/yidong/'; #folder
cookie='' #enter your cookie
userid='5699607196'; #enter your uid here
f = codecs.open(folder + 'desert.htm', 'r', 'utf-8');
html = f.read();
f.close();
root = etree.HTML(html)
tree = etree.ElementTree(root);
listnode=tree.xpath('//*[@id="feedList"]');
numre=re.compile(u'骑行|跑步|公里|,|耗时|消耗|大卡');
urllists=[]
records=[];
for child in listnode[0].iterchildren():
record={};
temp=child.xpath('div[2]/div[1]/a[2]')
if len(temp)==0:
continue;
source= temp[0].attrib['href'];
record['id']=source.split('/')[-1];
info=temp[0].text;
numinfo= numre.split(info);
if len(numinfo)<6:
continue;
record['type']= info[0:2];
record['distance']= numinfo[1];
record['hot']=numinfo[6];
urllists.append('http://edooon.com/user/%s/record/export?type=gpx&id=%s' % (userid, record['id']));
records.append(record)
alljson = json.dumps(records, indent=2, ensure_ascii=False);
codecs.open(folder+'info.json', 'w').write(alljson);
opener = urllib.request.build_opener()
opener.addheaders.append(('Cookie', cookie));
path='//*[@id="exportList"]/li[1]/a';
for everyURL in urllists:
id = everyURL.split('=')[-1];
print(id);
url='http://edooon.com/user/%s/record/%s' % (userid, id);
f = opener.open(url);
html = f.read();
f.close();
root = etree.HTML(html)
tree = etree.ElementTree(root);
fs = str(tree.xpath(path)[0].attrib['href']);
if fs is None:
continue;
furl = 'http://edooon.com/user/%s/record/%s' % (userid, fs);
f = opener.open(furl);
html = bytes.decode(f.read());
html=html.replace('</trkpt>','</trkpt>\n').replace('</time>','</time>\n').replace('<time>','\n<time>')
f.close();
filename=folder+id+'.gpx';
xmlfile = codecs.open(filename, 'w');
xmlfile.write(html);
xmlfile.close();
print ('all finished!')