Created
September 15, 2016 04:02
-
-
Save CapnKernel/eb649c3579673bb35dabf425bec8f10d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import requests | |
from lxml import html, etree | |
def get_shipment(item): | |
"Find the courier waybill number for this item" | |
print ">>>in get_shipment" | |
print ">>>item=", item | |
supplier_code = item.product.supplier_code | |
print ">>>supplier_code=", supplier_code | |
if not supplier_code: | |
print "returning None because there's no supplier code. board not pushed?" | |
return None | |
params = { | |
'id': supplier_code, | |
} | |
r = rs.get("%s%s" % (pcb_host, pcb_delivery_url), params=params) | |
r.raise_for_status() | |
print "status_code=", r.status_code | |
t = r.text | |
# print "text=", t | |
f = open("tmp/pcb-shipment.html", "w") | |
f.write(t) | |
f.close() | |
# Retrieve the courier waybill number | |
tree = html.document_fromstring(str(t)) | |
waybill_node = tree.xpath(u'//td[contains(.,"快递单号")]/following-sibling::td') | |
assert len(waybill_node) == 1 | |
waybill_num_text = waybill_node[0].text | |
if not waybill_num_text: | |
# Perhaps this item hasn't shipped yet. In this case, it'll | |
# have a production number, but no kuaidi code. We already know | |
# the kuaidi code is blank. Does it have a production number? | |
prodcode_node = tree.xpath(u'//td[contains(.,"内部编号")]/following-sibling::td') | |
assert len(prodcode_node) > 0 | |
prodcode = prodcode_node[0].text | |
if re.match('\d{3,5}-\d{4,6}', prodcode): | |
# Yes, prodcode. Almost certain it hasn't shipped. | |
raise ValueError, "Item %s probably hasn't shipped" % item | |
assert False, "Error fetching waybill for item %s" % item | |
waybill_num = waybill_num_text.strip() | |
print "waybill_num=", waybill_num | |
company_node = tree.xpath(u'//td[contains(.,"快递公司")]/following-sibling::td') | |
assert len(company_node) == 1 | |
company_name = company_node[0].text.strip() | |
print "company_name=", company_name | |
return (waybill_num, company_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment