5

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.

6
  • 1
    As your tag says, you know you should be using BeautifulSoup, but then why is your code using json.loads(URL)? Please check json document, it does not do what you think it does Commented Dec 16, 2016 at 20:23
  • so you want to extract data from within the script? You would first need to isolate it I.E. get just the string {".....data......:"} then use that with json.loads Commented Dec 16, 2016 at 20:25
  • @MoinuddinQuadri, I couldn't get it working with bs4 Commented Dec 16, 2016 at 20:32
  • @TadhgMcDonald-Jensen got it, thanks! Commented Dec 16, 2016 at 20:32
  • @TadhgMcDonald-Jensen How can I specify that data area? Commented Dec 16, 2016 at 20:34

1 Answer 1

14

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])
Sign up to request clarification or add additional context in comments.

1 Comment

Works pretty fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.