Skip to content

Instantly share code, notes, and snippets.

@ssebastianj
Created January 28, 2013 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssebastianj/4655061 to your computer and use it in GitHub Desktop.
Save ssebastianj/4655061 to your computer and use it in GitHub Desktop.
Dado un código de seguimiento, obtiene los detalles del envío mediante OCA©.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Sebastián J. Seba
import requests
from BeautifulSoup import BeautifulSoup
def get_tracking_info(tracking_code):
payload = {"numero": tracking_code.strip()}
get_url = "https://www1.oca.com.ar/OEPTrackingWeb/detalleenviore.asp"
try:
tracking_html = requests.get(get_url, params=payload)
except requests.RequestException as e:
print e.args[1]
soup = BeautifulSoup(tracking_html.content)
labels = soup.findAll(attrs={'class': 'texto'})
sender = {'name': _clean(labels[4]),
'address': _clean(labels[6]),
'city': _clean(labels[8])}
sender['zip_code'], sender['province'] = _split_prov_and_cp(labels[10])
receiver = {'name': _clean(labels[5]),
'address': _clean(labels[7]),
'city': _clean(labels[9])}
receiver['zip_code'], sender['province'] = _split_prov_and_cp(labels[11])
status_lines = []
for i in xrange(14, len(labels), 2):
status_lines.append({'date': labels[i].text.strip(' '),
'status': labels[i + 1].text.strip(' ')})
tracking_info = {'refer_num': _clean(labels[0]),
'packages_qty': _clean(labels[1]),
'sender': sender,
'receiver': receiver,
'status_info': status_lines}
return tracking_info
def _clean(label):
return label.text.split(' ')[-1].decode('utf-8')
def _split_prov_and_cp(label):
return (i.strip(' ').strip() for i in label.text.split('-'))
if __name__ == "__main__":
tracking_code = raw_input("Tracking Code: ")
print get_tracking_info(tracking_code)
@ssebastianj
Copy link
Author

Dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment