txn*_*nxr 2 javascript squarespace node.js node-request
我正在尝试请求此网站,但我不断收到 400 Bad Request 错误。这段代码几乎适用于我尝试过的任何其他不是用 squarespace 构建的网站,所以我猜这就是问题所在。
var request = require('request');
var cheerio = require('cheerio');
var url = 'http://www.pond-mag.com/';
request(url, function(error, resp, body){
if(!error){
var $ = cheerio.load(body);
console.log(body);
}
});
Run Code Online (Sandbox Code Playgroud)
想通了只需要手动设置标题对象。这是修复它的代码,以防其他人遇到问题:
var options = {
url : 'http://www.pond-mag.com/',
headers: {
'User-Agent': 'request'
}
};
Run Code Online (Sandbox Code Playgroud)
然后,只需将选项 var 传递给请求而不是 url。
| 归档时间: |
|
| 查看次数: |
309 次 |
| 最近记录: |