Skip to content

Instantly share code, notes, and snippets.

@uolot
Last active December 25, 2015 07:09
Show Gist options
  • Save uolot/6936822 to your computer and use it in GitHub Desktop.
Save uolot/6936822 to your computer and use it in GitHub Desktop.
Poczta Polska - śledzenie wielu przesyłek naraz
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import requests
from lxml import etree
import codecs
def get_tracking_numbers():
with open('input.txt') as f:
lines = [line.strip() for line in f.readlines()]
return lines
def fetch_shipping_status(number):
u = 'http://sledzenie.poczta-polska.pl/wssClient.php'
response = requests.post(u, {'n': number})
html = response.content.decode('utf-8')
return parse_response(html)
def parse_response(html):
tree = etree.HTML(html)
texts = tree.xpath('//table[2]//text()')[4:]
data = [u'\t'.join(texts[i:i+4]) for i in xrange(0, len(texts), 4)]
return u'\n'.join(data)
def store_results(lines):
with codecs.open('output.txt', 'w', 'utf-8') as f:
f.write(lines)
def main():
numbers = get_tracking_numbers()
result = []
for n in numbers:
header = u'Przesyłka {0}'.format(n)
print header
status = fetch_shipping_status(n)
s = u'\n{0}\n{1}'.format(header, status)
result.append(s)
store_results(u'\n'.join(result))
if __name__ == '__main__':
main()
requests==2.0.0
lxml==3.2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment