Tips on inputting information into a json file once you have completed crawling with casperjs?

I have written code that extracts a large number of words (innerHTML) from various webpages.

and I am interested in directly inserting this data into a json file..

Below is the JavaScript code snippet...

var words = [];
var casper = require('casper').create();

function getWords() {
    var words = document.querySelectorAll('td.subject a');
    return Array.prototype.map.call(words, function(e) {
        return e.innerHTML;
    });
}

casper.start('http://www.todayhumor.co.kr/board/list.php?table=bestofbest', function() {                
    words = this.evaluate(getWords);
});

for (var i=2; i <=5; i++) {
    casper.thenOpen('http://www.todayhumor.co.kr/board/list.php?table=bestofbest&page='+i, function() {              
        words = words.concat(this.evaluate(getWords));
    });
}

casper.run(function() {
    // Display results
    this.echo(words.length + ' links found:').exit();
    this.echo(words.join('\n')).exit();
});

Additionally,

I execute this code through terminal using the following command!

username@wow:~/workspace/app/assets/javascripts $ casperjs application.js

The result could be something like (example)

150 words found:
apple
banana
melon
kiwi
citrus
watermelon
passionfruit
mango
orange
...

Therefore, I aim to insert this data into the "word" section of my json file (as shown in the sample json code below)

Moreover, other columns ("type": "fruit" and "spell":) should be automatically included

{ "my_initial_words": [
    {
    "type": "fruit",
    "word": "apple",
    "spell": "ap"
    },
    {
    "type": "fruit",
    "word": "banana",
    "spell": "ba"
    },
    {
    "type": "fruit",
    "word": "melon",
    "spell": "me"
    }   
]
}
----------------------------------------------------------------------------

Thank you for providing additional information! However, I need guidance on where to incorporate this code

Can you please clarify which part of the code you provided executes the process of "Saving the results to JSON file?" as I intend to read the json file (makeyourap.json) in my seeds.rb file as follows

require 'json'
file = File.open(Rails.root.join('db','makeyourap.json'))
contents = file.read
json = ActiveSupport::JSON.decode(contents)["my_initial_words"]

Answer №1

Is this the solution you were looking for?

function createTypeObject(name, category) {
  return {
    name: name,
    category: category,
    abbreviation: name.substr(0,2)
  };
}

var wordDescriptions = words.map(function (word) { 
   return createTypeObject(word, "fruit"); 
});

var finalOutput = {
  my_initial_words: wordDescriptions
};

var jsonData = JSON.stringify(finalOutput);
// For pretty printing, use JSON.stringify(finalOutput, null, "\t");

I trust that this clarifies things for you.

Answer №2

Using Casper to Write to a File

To create a file where you can both read and append content, you can follow this method:

var fs = require('fs');
var FILENAME = 'makeyourap.json';
function add_new_fruits(fruits) {
    var data;
    if ( fs.isFile(FILENAME) ) {
        data = fs.read(FILENAME);
    } else {
        data = JSON.stringify({'my_initial_words' : [] });
    }
    var json = JSON.parse(data);
    fruits.forEach(function(word) {
        json.my_initial_words.push({"type": "fruit",
                                    "name": word,
                                    "spell": word.slice(0,2)});
    });
    data = JSON.stringify(json, null, '\t');
    fs.write(FILENAME, data, "w");
}

Replace the older this.echo with this method. Simply call it like this:

casperjs application.js

This will either read the object from the file, or create it if it doesn't exist. Then, it will append each new object from the new fruits (even duplicates), and write it back to the FILENAME.

Previous Method: Creating Your Own Object

Create Object

Start by creating an object that only contains the parameter my_initial_words with values as specified.

You can use the following function:

function createFinal(wordArray) {
    var out = [];
    wordArray.forEach(function(word) {
        out.push({"type": "fruit", "name": word, "spell": word.slice(0,2)});
    });
    return out;
}

to generate the array. Then, create the object with:

var my_object = { "my_initial_words": createFinal(words) };

Converting to JSON

JavaScript has a built-in JSON-object. With a JavaScript object defined as

var my_object = { "my_initial_words": ...

as shown above, you can use

JSON.stringify(my_object) 

to obtain the JSON representation for writing purposes.

Previous Method: Redirecting Output to File

Prior to the current approach, you may have used

this.echo(words.join('\n')).exit();

to display the list. Instead of using this.echo, consider replacing it with

var my_object = { "my_initial_words": createFinal(words) };
this.echo(JSON.stringify(my_object)).exit();

This will output to standard output. Delete the other this.echo line and redirect the output by running

casperjs application.js > makeyourap.json

If you need assistance with writing to a file in casperjs, refer to this guide.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Volley's request queue cannot be utilized within a View Model

Having trouble loading my JSON data into a view model class that extends the Android ViewModel. I'm unable to get the context for the Request Queue. The view model class that extends the Android ViewModel will be utilized by an observer to load the p ...

The process of deserializing JSON data in VB.Net

I have a JSON data that I need to convert back into objects. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim client As New RestClient(BaseUrl) Dim Respons As Object client.Authenticator = O ...

Sticky sidebar panel featuring a stationary content block

I have a sidebar that is set to position:fixed; and overflow:auto;, causing the scrolling to occur within the fixed element. When the sidebar is activated, the element remains static on the page without any movement. My goal: I am looking to keep the .su ...

Create a separate child process in Node.js

Is it possible to create a separate process from the current script? I want to execute another script while the original one is still running. This new script should be completely independent of the calling script. ...

Issue with PHP causing Jquery alert to trigger twice rather than once

I am currently working with jQuery and PHP. I have a button labeled "Edit" but whenever I click on it, the alert message pops up twice instead of just once. Below is my HTML code within the PHP tags: <?php $PostComment2='<div class="button1 ...

Tips for getting rid of Next.js' default loading indicator

While working on my project using Next.js, I've noticed that whenever I change the route, a default loading indicator appears in the corner of the screen. https://i.sstatic.net/FVWEU.gif Does anyone know how to remove this default loading indicator ...

Is it possible to switch out all instances of "GET" methods with "POST" throughout the codebase?

While working on a web application, we encountered caching issues with Internet Explorer (caching occurred due to the use of GET requests). The problem was resolved when users turned on "Always refresh from server" in IE's Developers Tool. Although we ...

Can you explain the purpose of the role attribute in XHTML? How is it commonly utilized?

After reviewing W3C's information about the role attribute, I am still unclear about its purpose. Is the role attribute meant to simply clarify the code, or do some browsers or spiders interpret it in a specific way? Could the role attribute serve as ...

Receiving inaccurate video duration when the input bar is modified

Initially, I am uncertain whether the issue stems from Javascript or CSS. The code in question involves displaying the corresponding video time when a user hovers over an input bar. The calculation utilized is as follows: ((user mouseX position / input wi ...

I need to show the JSON object value within an Android activity

I recently delved into the world of Android development and have been trying to fetch data from a database in JSON format. However, the return value I am receiving looks like "{"users":[{"child_name":"John"}]}" when I use the following code snippet: publi ...

I'm having trouble figuring out how to access response headers with HttpClient in Angular 5. Can anyone

I recently developed an authentication service in Angular 5, where I utilize the HttpClient class to make a POST request to my backend server. The backend server then responds with a JWT bearer token. Here is a snippet of how my request looks: return thi ...

How can a child class access this.props within a function that overrides a parent class's function?

I am trying to access this.props.childName in the child function, which is defined within the parent function. However, I am encountering a TypeScript compile error (Property 'name' does not exist...). Strangely, if I use this.props.parentName, i ...

Showing data retrieved from a JSON file on an Android device

Within my project, I have a json file stored in the raw folder that contains the following code (among other things): { "Monday": [ { "time": "09:15", "class": "Nature", "room": "AL32" }, { "time": "10:15", ...

Using " " to split a name into two lines is not being recognized

My issue involves the display of tab names in two lines within multiple tabs. You can view the demonstration here. I attempted to use the \n character while setting the tab name but it was not recognized. Any suggestions on how to achieve this? Here ...

Issue with transmitting Razor form data to API controller using fetch or AJAX

I have created a basic razor web project and defined it as follows: in program.cs I added builder.Services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN"); In the controller, this is what I did: [Route("/api/[controller]")] [ApiCon ...

JavaScript debugging causing system freeze

Currently, I am working on a project that involves using MVC and dropdown lists. My issue arises when the dropdown list changes, as there is some javascript code that needs to execute. To troubleshoot the problem of the system locking up every time I tried ...

"The sliding function of the React Bootstrap carousel is malfunctioning as it goes blank just before transitioning

Here is the code snippet I am working with: Whenever the carousel transitions to the next image, the current image disappears before displaying the next one. I am using react-bootstrap version 5.1.0, but it seems like there may be an issue with the transi ...

Retrieve a specific row from a table in Bootstrap

Is there a way to highlight a row in a table by clicking on it using CSS classes? $("#infoTable tr").click(function() { var selected = $(this).hasClass("highlight"); $("#infoTable tr").removeClass("highlight"); if (!selected) $(this).addClass( ...

Importing classes in ECMAScript 6 does not work properly, causing issues when running scripts

I am currently learning Selenium Webdriver. I am facing an issue where I can't run a script with imported classes, but I am able to run it without classes using import functions only. To execute it, I use babel-cli in the following manner: node ./babe ...

Use of absolute positioning resulted in the disappearance of the element

Can anyone assist with resolving the issue I am encountering? I currently have three nested divs as follows: <div class="section"> <div class="parent"> <div class="child"> Some random text. </div> </div> </div> To adj ...