Last active
April 12, 2025 13:41
-
-
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©.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# /// script | |
# requires-python = ">=3.9" | |
# dependencies = [ | |
# "beautifulsoup4>=4.13.3", | |
# "requests>=1.1.0", | |
# ] | |
# /// | |
# -*- coding: utf-8 -*- | |
# Author: Sebastián J. Seba | |
import requests | |
from bs4 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, "html.parser") | |
labels = soup.find_all(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 range(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 = input("Tracking Code: ") | |
print(get_tracking_info(tracking_code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment