Skip to content

Instantly share code, notes, and snippets.

@s-celles
Forked from bertini36/webscraping.py
Created February 27, 2022 07:54
Show Gist options
  • Save s-celles/823b674fe1922d2fb510c6bc8c67f839 to your computer and use it in GitHub Desktop.
Save s-celles/823b674fe1922d2fb510c6bc8c67f839 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
# coding=utf-8
import requests
# Track ids to download
tracks = [
'9319946',
'9319955',
...
]
# Different users to login
users = [
{"email":"[email protected]", "password":"password"},
{"email":"[email protected]", "password":"password"},
...
]
# Different proxys to download tracks
proxies = [
{"http": "http://XX.XX.XX.XX:PP/"},
{"http": "http://YY.YY.YY.YY:PP/"},
...
]
def login(credentials):
login_url = 'http://es.wikiloc.com/wikiloc/login.do'
headers = {
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'es.wikiloc.com'
}
session = requests.session()
session.post(login_url)
session.post(login_url, data=credentials, headers=headers)
return session
def downloadGpx(session, track_id, proxy):
download_url = 'http://es.wikiloc.com/wikiloc/downloadToFile.do?event=download&id={}&format=gpx'.format(track_id)
response = session.get(download_url, stream=True, proxies=proxy)
status_code = response.status_code
if status_code == 200:
f = open("gpx/" + track_id + ".gpx", 'wb')
inside = False
for chunk in response.iter_content(chunk_size=1024):
inside = True
if chunk:
f.write(chunk)
f.close()
if inside == False: status_code = -1
return status_code
def downloadTracks():
user = 0
proxy = 0
session = login(users[user])
for track_id in tracks:
try:
status_code = downloadGpx(session, track_id, proxies[proxy])
except:
status_code = 0
pass
while status_code != 200:
if status_code == -1:
user = 0 if user == len(users)-1 else user = user + 1
session = login(users[user])
else:
proxy = 0 if proxy == len(proxies)-1 else proxy = proxy + 1
try:
status_code = downloadGpx(session, track_id, proxies[proxy])
except:
status_code = 0
pass
print "CHIM-PUM"
def main():
downloadTracks()
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment