Skip to content

Instantly share code, notes, and snippets.

@adamgreig
Created July 29, 2014 16:57
Show Gist options
  • Save adamgreig/0dcf5c24b71fc3ed1394 to your computer and use it in GitHub Desktop.
Save adamgreig/0dcf5c24b71fc3ed1394 to your computer and use it in GitHub Desktop.
Turn a CSV number,qty of Farnell parts into a txt of name,number,url,qty,unit cost,total cost
import requests
from lxml import html
import json
parts = {}
with open("farnell_bom_condensed.txt") as f:
for line in f:
number, qty = line.split(", ")
qty = int(qty)
url = "http://uk.farnell.com/_/_/dp/{}".format(number)
req = requests.get(url)
tree = html.fromstring(req.text)
title = tree.xpath('//*[@id="headerContainer"]/h1/text()')[0]
price = tree.xpath(
'//*[@class="nowrap mfProductDescriptionAndPrice"]/text()')[2]
parts[number] = {"qty": qty, "url": url, "number": number,
"name": title.strip(), "price": price.strip()[1:],
"cost": float(price.strip()[1:]) * int(qty)}
print(parts[number])
with open("farnell_detailed.json", "w") as f:
json.dump(parts, f)
with open("farnell_detailed.txt", "w") as f:
for number in parts:
part = parts[number]
f.write("{name},{number},{url},{qty},{price},{cost}\n".format(**part))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment