admin管理员组文章数量:1026989
I try to send a request to an Apache server and use the returned body.
Follow the manual of nodejs .html#https_https_request_options_callback
and related SO content How to make external HTTP requests with Node.js
My src is
callback = function(response) {
body='';
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.on('data', function (chunk) {
body+= chunk;
});
response.on('end', function () {
console.log(body);
});
}
My question is that the body here will include all the HTML header tag, such as <!DOCTYPE html>
...etc, which cannot be parsed by JSON object. Because all my data in the HTML body is JSOn, I want to get only the HTML body. Are there anyway to achieve this goal? Thanks in advance.
I try to send a request to an Apache server and use the returned body.
Follow the manual of nodejs http://nodejs/api/https.html#https_https_request_options_callback
and related SO content How to make external HTTP requests with Node.js
My src is
callback = function(response) {
body='';
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.on('data', function (chunk) {
body+= chunk;
});
response.on('end', function () {
console.log(body);
});
}
My question is that the body here will include all the HTML header tag, such as <!DOCTYPE html>
...etc, which cannot be parsed by JSON object. Because all my data in the HTML body is JSOn, I want to get only the HTML body. Are there anyway to achieve this goal? Thanks in advance.
- 1 I am confused. Whatever returned is already HTML only, right? – thefourtheye Commented Dec 24, 2013 at 4:42
- 1 response body is not HTML page body. You would have to scrape the page to extract <body> contents and then JSON.parse it. – user568109 Commented Dec 24, 2013 at 5:00
- There are some package for html query in node.js: cherio or jquery. github./MatthewMueller/cheerio – damphat Commented Dec 24, 2013 at 5:31
- @thefourtheye You are right, but I am trying to get the 'body' of HTML – StevenR Commented Dec 24, 2013 at 6:04
- @vkurchatkin Sorry, that is a mistake. – StevenR Commented Dec 24, 2013 at 6:06
1 Answer
Reset to default 4I am not giving plete code here.
You can use modules like
htmlparser2
to extract the data from HTML's body (Online demo for the same
).And then you can use
JSON.parse
to parse the extracted string to a JSON Object.
I try to send a request to an Apache server and use the returned body.
Follow the manual of nodejs .html#https_https_request_options_callback
and related SO content How to make external HTTP requests with Node.js
My src is
callback = function(response) {
body='';
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.on('data', function (chunk) {
body+= chunk;
});
response.on('end', function () {
console.log(body);
});
}
My question is that the body here will include all the HTML header tag, such as <!DOCTYPE html>
...etc, which cannot be parsed by JSON object. Because all my data in the HTML body is JSOn, I want to get only the HTML body. Are there anyway to achieve this goal? Thanks in advance.
I try to send a request to an Apache server and use the returned body.
Follow the manual of nodejs http://nodejs/api/https.html#https_https_request_options_callback
and related SO content How to make external HTTP requests with Node.js
My src is
callback = function(response) {
body='';
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.on('data', function (chunk) {
body+= chunk;
});
response.on('end', function () {
console.log(body);
});
}
My question is that the body here will include all the HTML header tag, such as <!DOCTYPE html>
...etc, which cannot be parsed by JSON object. Because all my data in the HTML body is JSOn, I want to get only the HTML body. Are there anyway to achieve this goal? Thanks in advance.
- 1 I am confused. Whatever returned is already HTML only, right? – thefourtheye Commented Dec 24, 2013 at 4:42
- 1 response body is not HTML page body. You would have to scrape the page to extract <body> contents and then JSON.parse it. – user568109 Commented Dec 24, 2013 at 5:00
- There are some package for html query in node.js: cherio or jquery. github./MatthewMueller/cheerio – damphat Commented Dec 24, 2013 at 5:31
- @thefourtheye You are right, but I am trying to get the 'body' of HTML – StevenR Commented Dec 24, 2013 at 6:04
- @vkurchatkin Sorry, that is a mistake. – StevenR Commented Dec 24, 2013 at 6:06
1 Answer
Reset to default 4I am not giving plete code here.
You can use modules like
htmlparser2
to extract the data from HTML's body (Online demo for the same
).And then you can use
JSON.parse
to parse the extracted string to a JSON Object.
本文标签: javascriptAnyway I can get the html body in nodejsStack Overflow
版权声明:本文标题:javascript - Anyway I can get the html body in node.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745657366a2161676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论