I built a web scraper for this page that hinged on parsing a string as JSON file. But they've made some updates to the site and now the scraper has stopped working. I think the issue is that the information I need is no longer structured as JSON.
Here's what I had originally:
# Packages
from bs4 import BeautifulSoup
from urllib.request import urlopen, urlretrieve
import json
import ast
# The part that still works
address = 'https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-1-python-basics?ex=2'
html = urlopen(address)
soup = BeautifulSoup(html, 'lxml')
string = soup.find_all('script')[2].string
json_text = string.strip('window.PRELOADED_STATE = "')[:-2]
# The part that's now broken
lesson = json.loads(json_text)
#> Traceback (most recent call last):
#> <ipython-input-11-f9b7d249d994> in <module>()
#> 2 # The part that's now broken
#> 3
#> ----> 4 lesson = json.loads(json_text)
#> ~/anaconda3/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
#> 352 parse_int is None and parse_float is None and
#> 353 parse_constant is None and object_pairs_hook is None and not kw):
#> --> 354 return _default_decoder.decode(s)
#> 355 if cls is None:
#> 356 cls = JSONDecoder
#> ~/anaconda3/lib/python3.6/json/decoder.py in decode(self, s, _w)
#> 337
#> 338 """
#> --> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
#> 340 end = _w(s, end).end()
#> 341 if end != len(s):
#> ~/anaconda3/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
#> 355 obj, end = self.scan_once(s, idx)
#> 356 except StopIteration as err:
#> --> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
#> 358 return obj, end
#> JSONDecodeError: Expecting value: line 1 column 2 (char 1)
The issue is that all the information in json_text
is no longer structured as a JSON.
need_to_parse = BeautifulSoup(json_text, 'lxml').string #Escape HTML
print(len(need_to_parse))
#> 61453
print(need_to_parse[:50])
#> ["~#iM",["preFetchedData",["^0",["course",["^0",["
print(need_to_parse[-50:])
#> "type","MultipleChoiceExercise","id",14253]]]]]]]]
I thought maybe is was a nested list, so I tried ast.literal_eval()
, but no luck!
parsed_list = ast.literal_eval(need_to_parse)
#> Traceback (most recent call last):
#> File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
#> exec(code_obj, self.user_global_ns, self.user_ns)
#> File "<ipython-input-13-55b60da762d6>", line 2, in <module>
#> parsed_list = ast.literal_eval(need_to_parse)
#> File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/ast.py", line 48, in literal_eval
#> node_or_string = parse(node_or_string, mode='eval')
#> File "/Users/nicholascifuentes-goodbody/anaconda3/lib/python3.6/ast.py", line 35, in parse
#> return compile(source, filename, mode, PyCF_ONLY_AST)
#> File "<unknown>", line 1
#> ["~#iM",["preFetchedData"
The full output is in a txt
file HERE.
Does anyone recognize this data structure? What's the best way to parse it?
Created on 2018-10-19 by the reprexpy package
import reprexpy
print(reprexpy.SessionInfo())
#> Session info --------------------------------------------------------------------
#> Platform: Darwin-17.7.0-x86_64-i386-64bit (64-bit)
#> Python: 3.6
#> Date: 2018-10-19
#> Packages ------------------------------------------------------------------------
#> beautifulsoup4==4.6.0
#> reprexpy==0.1.1