0

I am looking to extract fantasy football information from a website. I can write enough code to get the following output but all I really want is the following information:

"fullName":"Justin Forsett"
"pointsSEASON":75

Can anyone help explain how to isolate these items and write them to, for example, a csv file?

[<div class="mod-content" id="fantasy-content">{"averagePoints":9.4,"percentOwned":98.6,"pointsSEASON":75,"seasonOutlook":{"outlook":"Forsett finished 2014 as fantasy's No. 8 RB, so why aren't we higher on him? Well, it's difficult to reconcile what we know about his size (5-8, 197), age (30 in October) and career with the 1,529 scrimmage yards he racked up as Baltimore's surprise starter. Forsett had never even eclipsed 1,000 total yards in any of his six previous seasons. Yet his quickness and vision were consistently excellent last year, and new OC Marc Trestman loves throwing to RBs. Lorenzo Taliaferro and rookie Javorius Allen loom as heftier options, and some kind of rotation could develop. But Forsett will get the benefit of the doubt in Week 1.","seasonId":2015,"date":"Wed May 20"},"positionRank":18,"playerId":11467,"percentChange":-0.2,"averageDraftPosition":42.5,"fullName":"Justin Forsett","mostRecentNews":{"news":null,"spin":"The Jaguars have allowed the second-fewest yards per carry (3.4) in the league, but have ceded one rushing score per game in the process. Forsett will need a good deal of volume to overcome a quietly tough matchup, but we're trusting the workload will be enough.","date":"Tue Nov 10"},"totalPoints":75,"projectedPoints":13.957546548,"projectedDifference":4.582546548}</div>]
senshin
  • 10,022
  • 7
  • 46
  • 59

1 Answers1

0

It looks like the text of the tag you are looking for is in JSON format. You have successfully gotten the div tag, but now you have to extract the JSON, and then extract the information you want. Here is what you will need to add to your code.

import json

rawJSONString = {originaltag}.get_text()
JSONString = json.loads(rawJSONString)
print(JSONString['fullName'])
print(JSONString['pointsSEASON'])

{originaltag} is the tag you printed up above, since you didn't show your code, I couldn't run it. Instead I ran the following code

string = '{"averagePoints":9.4,"percentOwned":98.6,"pointsSEASON":75,"seasonOutlook":{"outlook":"Forsett finished 2014 as fantasys No. 8 RB, so why arent we higher on him? Well, its difficult to reconcile what we know about his size (5-8, 197), age (30 in October) and career with the 1,529 scrimmage yards he racked up as Baltimores surprise starter. Forsett had never even eclipsed 1,000 total yards in any of his six previous seasons. Yet his quickness and vision were consistently excellent last year, and new OC Marc Trestman loves throwing to RBs. Lorenzo Taliaferro and rookie Javorius Allen loom as heftier options, and some kind of rotation could develop. But Forsett will get the benefit of the doubt in Week 1.","seasonId":2015,"date":"Wed May 20"},"positionRank":18,"playerId":11467,"percentChange":-0.2,"averageDraftPosition":42.5,"fullName":"Justin Forsett","mostRecentNews":{"news":null,"spin":"The Jaguars have allowed the second-fewest yards per carry (3.4) in the league, but have ceded one rushing score per game in the process. Forsett will need a good deal of volume to overcome a quietly tough matchup, but were trusting the workload will be enough.","date":"Tue Nov 10"},"totalPoints":75,"projectedPoints":13.957546548,"projectedDifference":4.582546548}'
s = json.loads(string)
print(s['fullName'])
print(s['pointsSEASON'])

And got this output

Justin Forsett
75

Edited to add: Here is information on writing to a csv file.

Community
  • 1
  • 1
dstudeba
  • 8,878
  • 3
  • 32
  • 41
  • Thanks for the response, I tried to take what you provided and add it to my code but it pulled up an error message related to the 'get_text()' section. In my code the variable for the tag printed above is 'tag'. All I did was replace {originaltag} with tag and that generated the following error: ResultSet object has no attribute 'get_text()'. Any help on what I am missing would be awesome. Thanks again. – user2601008 Nov 21 '15 at 23:41
  • Please add the code to your original question, it is much easier to debug. That error usually happens when you have a string instead of a tag, so try running it without the `.get_text()` method. – dstudeba Nov 21 '15 at 23:55