Nodejs - Get content from external website

浪尽此生 提交于 2020-01-03 04:26:05

问题


i wrote a simple website that returns a random 4-digit number every time you reload. Okay so i want to read that digit through Node.js.. Googled couple times and found out that axios and fs was my best bet..

My website: SOLVED

The website above works on my browser.. might not on yours.. so i made a temp website for this.. it still doesn't work as expected: SOLVED

I came up with this:

let axios = require('axios');
let cheerio = require('cheerio');
let fs = require('fs'); 

axios.get('http://thedico.com/third.php')
    .then((response) => {
        if(response.status === 200) {
          const html = response.data;
          const $ = cheerio.load(html); 
            res.send("Status returned 200...!");
    }
    }, (error) => console.log(error) res.send("error...!"));

    res.send("Done...!");

I use this code with Google Cloud Functions a.k.a gCloud but this code nor prints a output or returns my website data.. What should i do?

Things i made sure of:

  • My website is up and loading time is around 440MS on normal speed.
  • My 'gCloud' billing is set and enabled
  • everything works fine on manual reload

Thanks


回答1:


I tried with axios and request as well it is working fine now. As well you have a couple of bugs in your code.

const request = require("request");
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs');

with Request module

request('https://alexandrarawand.000webhostapp.com/index.php', function (error, response, html) {
    if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        // Get text 
        console.log("------- with request module -------")
        console.log($.text());
        // Get HTML 
        //console.log($.html());
    }
});

with Axios module

axios.get('https://alexandrarawand.000webhostapp.com/index.php')
    .then((response) => {
        if (response.status === 200) {
            const html = response.data;
            const $ = cheerio.load(html);
            // Get text 
            console.log("------- with axios module -------")
            console.log($.text());
            // Get HTML 
            //console.log($.html());
        }
    })
    .catch((err) => {
        throw new Error(err);
    });

Hope it may help you.



来源:https://stackoverflow.com/questions/50075001/nodejs-get-content-from-external-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!