-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrapCommodities.py
More file actions
executable file
·28 lines (21 loc) · 1.05 KB
/
ScrapCommodities.py
File metadata and controls
executable file
·28 lines (21 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
from lxml import html
import requests
import datetime
gold_url = "https://www.investing.com/commodities/gold-historical-data"
silver_url = "https://www.investing.com/commodities/silver-historical-data"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
def scrapData(url, comodity):
page = requests.get(url, headers=headers)
tree = html.fromstring(page.content)
entries = tree.xpath('//div[@id="results_box"]/table[@class="genTbl closedTbl historicalTbl"]/tbody/tr')
with open('{}.csv'.format(comodity), 'w+') as csvfile:
for entry in entries:
tds = entry.xpath('td/text()')
date_string = tds[0]
price = tds[1].replace(",", "")
date = datetime.datetime.strptime(date_string, '%b %d, %Y')
formatted_date = date.strftime('%y-%m-%d')
csvfile.write("{}\t{}\n".format(formatted_date, price))
scrapData(gold_url, "gold")
scrapData(silver_url, "silver")