0

I want to crawl the app info based on app id (e.g. com.instagram.android) in Google Play.

I used the npm package as the crawler: https://www.npmjs.com/package/google-play-scraper

I don't have any javascript experience before, I don't know how to modify it for my purpose. For now, I cannot use that for crawling a list of app IDs, and save the returned results as a *.txt for further analysis.

I have tried their sample code, and it runs well. But I could only see the returned results by .then(console.log, console.log). How could I save the results to a .txt file???

var gplay = require('google-play-scraper');

gplay.app({appId: 'com.dxco.pandavszombies'})
  .then(console.log, console.log);

I am stuck here, could anyone help me on this issue? I have tried many different ways to solve this problem, but they all didn't work... I am really confused about how to save the value returned to .txt file

I tried to save all the potential variables into the .txt files, only the info like:

[object]
[object promise]

are saved.

I tried to save it like this:

var first = gplay.app({appId: 'com.instagram.android'});
app_result = first.then((result) => console.log(result));

fs.writeFile('App_Result.txt', app_result, (err) =>{
    if (err) throw err;
        console.log('App_Result saved!');
    });

I understand that this may be caused by the strategy of Promise, which I need to wait for the Promise resolved and then the results could be returned. But I don't know how to figure out it since I am really new for the javascript.

Yvonne.Tian
  • 139
  • 2
  • 9
  • How are you saving the values to the `.txt` files? – Einar Ólafsson Jun 26 '19 at 17:15
  • You need to convert the object to a string. `JSON.stringify(app_result)` – Einar Ólafsson Jun 26 '19 at 17:17
  • Possible duplicate of [Convert JS object to JSON string](https://stackoverflow.com/questions/4162749/convert-js-object-to-json-string) – Einar Ólafsson Jun 26 '19 at 17:18
  • @EinarÓlafsson I tried to save different variables (e.g. console.log, result, app_result), I added one way I tried in the post. Please check it. Thanks a lot. – Yvonne.Tian Jun 26 '19 at 17:19
  • @EinarÓlafsson Yes. I have tried the JSON.stringfy(app_result), it only save "{}" to the .txt file – Yvonne.Tian Jun 26 '19 at 17:21
  • You are stringifying a Promise that has not yet resolved. See my answer shortly. – joshwilsonvu Jun 26 '19 at 17:22
  • @EinarÓlafsson It seems like I need to wait for the Promise solved, but how Can I save it instead of only viewing in the console. – Yvonne.Tian Jun 26 '19 at 17:23
  • @jwilson Thanks a lot. Yes. It seems like that. I also checked the Promise definition things before, but since I am really new in the .js. I could not figure out a way to solve it. I have been stuck here for a whole afternoon. T_T – Yvonne.Tian Jun 26 '19 at 17:25

2 Answers2

2

First: JavaScript by default prints objects very unhelpfully, something like [Object object] or similar. If you need more information, try replacing console.log with data => JSON.stringify(data).

Second, due to the nature of Promises, you'll have to chain another .then onto your existing code. Something like

const gplay = require('google-play-scraper');
const fs = require('fs');

const file = fs.createWriteStream('App_Result.txt');

gplay.app({appId: 'com.dxco.pandavszombies'})
  .then(result => JSON.stringify(result))
  .then(text => file.write(text));
joshwilsonvu
  • 2,569
  • 9
  • 20
0

Your app_result will not equal the result of the promise but rather the promise itself.

You also have to convert the object to a string using JSON.stringify before you can write it to a file.

Try writing an async function like this.

const myFunction = async () => {
    const app_result = await gplay.app({appId: 'com.instagram.android'});

    fs.writeFile('App_Result.txt', JSON.stringify(app_result))
}
Einar Ólafsson
  • 3,036
  • 1
  • 19
  • 24