Interested to know if there is a better or another way to handle URLs that contain only JSON data using Nightmare.js than using document.querySelector('*').textContent
within .evaluate?
Here is an example; the external URL here contains the following which is the contents of a linked select field
{
"baseDeliveryModelId":1,
"county": [
{"id": "1000706000", "label": "Çukurova", "value": "Çukurova"},
{"id": "1000707000", "label": "Sarıçam", "value": "Sarıçam" },
{"id": "1000922000", "label": "Seyhan", "value": "Seyhan"},
{"id": "1000921000", "label": "Yüreğir","value": "Yüreğir"}
],
"listType":"DISTRICT_LIST"
}
A sample.js code to retrieve just the county data from the URL (which works fine)
const Nightmare = require('nightmare'),
vo = require('vo'),
nightmare = Nightmare({show: true});
function counties(city) {
let countyUrl = `https://www.sanalmarket.com.tr/kweb/getCityDeliveryLocation.do?shopId=1&locationId=${city}&locationType=city&deliveryTypeId=0`;
return nightmare
.goto(countyUrl)
.evaluate(function() {
return (JSON.parse(document.querySelector('*').textContent)).county;
})
.catch(function (err) {
console.log('Error: ', err);
});
}
vo(function* () {
return yield counties('01');
})((err, result) => {
if (err) return console.log(err);
console.log(result);
});
Note: The question is about using Nightmare.js , or using other libraries with Nightmare.js in node.js for handling JSON responses, I am fully aware and capable of using other libraries such as axios.js on their own to solve the above.
You don't need nightmarejs. if you can use a library that auto parse the json response for you, for example request-promise
const rp = require('request-promise');
rp({
url: 'https://www.sanalmarket.com.tr/kweb/getCityDeliveryLocation.do?shopId=1&locationId=${city}&locationType=city&deliveryTypeId=0',
json: true
}).then(function (data) {
console.log(data.country);
})
.catch(function (err) {
// Crawling failed...
});
This is how I did it, it's faster to implement, easier to remember. We can use it like this until someone creates functions like .text() and .json() .
// Initiate nightmare instance
var nightmare = Nightmare({
show: true,
alwaysOnTop: false
})
// go to a page with json response
.goto('https://api.ipify.org/?format=json')
.evaluate(() => {
// since all of the text is just json, get the text and parse as json, return it.
return JSON.parse(document.body.innerText)
})
.then((data) => {
// then use it however we want
console.log(data)
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.