0

I am trying to build a program to scrape some Twitter data and then show them in a user-friendly format on a webpage (title and content). So far I have created the scraper and I have saved the data as a JSON file.

for tweet in new_tweets:
    outtweets=tweet._json
    with open('Trumpstweets.json', 'a') as f:
        print(outtweets)
        f.write(json.dumps(outtweets))

And this is a part of my huge JSON file.

{"id": 1193249691741216768, "created_at": "Sat Nov 09 19:31:10 +0000 2019", "contributors": null, "id_str": "1193249691741216768", "in_reply_to_screen_name": null, "geo": null, "entities": {"user_mentions": [], "symbols": [], "hashtags": [{"text": "MAGA", "indices":

I am not sure how to show this data to a user. I am very new to Python and JSON. I am trying to show it on a webpage.

<head>
    <title>askyb - Load JSON File Locally by Javascript Without JQuery</title>
    <script type="text/javascript" src="tweets.json"></script>
</head>

function load() {
    var mydata = JSON.parse(tweets);
    alert(mydata[0]);
    alert(mydata[1]);
}

and there is an error:

in tweets.json:1 Uncaught SyntaxError: Unexpected token ':'

Hanna
  • 539
  • 2
  • 9
  • 24
  • Well, you are certainly not going to present it to user as python or json. I would suggest html ;) Anyway, it is not clear what your problem is. Have you not heard of html, or you dont know how to run a web server, or you dont know how to parse json... In short, you have not asked a question (where, _"please tell me how to finish the remaining 95% of my job"_ is not a valid question) – zvone Nov 09 '19 at 23:54
  • @zvone I have edited my question. I thought it's clear as I am working with JSON and I am trying to show it on a webpage, I need to use some HTML and js :) – Hanna Nov 10 '19 at 00:01
  • 1
    [Parsing Twitter json](https://stackoverflow.com/questions/14856526/parsing-twitter-json-object-in-python). Afaik, getting data out of json is similar to getting data out of a python dict. [Another helpful link](https://stackoverflow.com/questions/20199126/reading-json-from-a-file) – cozek Nov 10 '19 at 00:06
  • 1
    This is a javascript question, FTR – pguardiario Nov 10 '19 at 00:08
  • @cozek I have done all those steps and I have my JSON file. I am not sure how to show them in HTML. – Hanna Nov 10 '19 at 03:34

1 Answers1

0

Just add var mydata = to the script. For example (tweets.js):

var mydata = {"id": 1193249691741216768, ...}
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • I do not want to put a huge JSON data in my script and I am getting this data from twitter so it is not static. – Hanna Nov 10 '19 at 00:57
  • 1
    You can do it in a xhr so it doesn't slow down your page load, but there's no other way to load json than to actually load the json. – pguardiario Nov 10 '19 at 01:15
  • You mean I need to have the whole data in my script rather than having a reference to the JSON file? @pguardiario – Hanna Nov 10 '19 at 01:18
  • If you want to access the data, then yes. – pguardiario Nov 10 '19 at 02:39