Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Javascript In Python

I usually use Beautiful Soup to parse html that I need, but I came across some Javascript that I would like to get from here.

 <script>
function Model(){
    this.players = [{".....data......:""}];...etc

I tried to load it like...

import json
scrape_url = "https://swishanalytics.com/optimus/nba/daily-fantasy-projections?date=2016-12-15"

result = json.loads(scrape_url)

But I get "No Json Can Be Decoded". Not sure how to go about this.

like image 862
Ravash Jalil Avatar asked Dec 08 '22 20:12

Ravash Jalil


1 Answers

You can extract JSON from arbitrary text with the jsonfinder library:

from jsonfinder import jsonfinder
import requests

scrape_url = "https://swishanalytics.com/optimus/nba/daily-fantasy-projections?date=2016-12-15"
content = requests.get(scrape_url).text
for _, __, obj in jsonfinder(content, json_only=True):
    if (obj and
            isinstance(obj, list) and
            isinstance(obj[0], dict) and
            {'player_id', 'event_id', 'name'}.issubset(obj[0])
            ):
        break
else:
    raise ValueError('data not found')

# Now you can use obj
print(len(obj))
print(obj[0])
like image 115
Alex Hall Avatar answered Jan 08 '23 08:01

Alex Hall