I'm trying to scrape the "Major Stock Indexes table" from https://markets.wsj.com/ and would like to save it to a folder on my desktop. This is what I have so far:
import urllib.request
import json
import re
html = urllib.request.urlopen("https://markets.wsj.com/").read().decode('utf8')
json_data = re.findall(r'pws_bootstrap:(.*?)\s+,\s+country\:', html, re.S)
data = json.loads(json_data[0])
filename = "C:\Users\me\folder\sample.csv"
f = open(filename, "w")
for numbers in data['chart']:
for obs in numbers['Major Stock Indexes']:
f.write(str(obs['firstCol']) + "," + str(obs['dataCol']) + "," + str(obs['dataCol priceUp']) + str(obs['dataCol lastb priceUp']) + "\n")
print(obs.keys())
I'm getting the error: IndexError: list index out of range
Any ideas what might fix my issue?