我正在尝试为纽约建筑数据库编写一个网络爬虫,并且我正在尝试获取实际网站的 html。无论出于何种原因,当我输入要抓取的网站的网址时,我的程序什么也不做。每当我输入几乎任何其他网站的 url 时,我实际上都会得到我请求的 html。这是因为我正在尝试抓取政府网站吗?
var request = require("request");
request(
{ uri: "http://a810-bisweb.nyc.gov/bisweb/JobsQueryByNumberServlet?requestid=3&passjobnumber=123768556&passdocnumber=01" },
function(error, response, body) {
console.log(body);
console.log("hello")
}
);
我希望收到 html 作为打印在控制台中的字符串,但我什么也没得到。 “你好”甚至没有打印出来。但是,当我尝试任何其他网站时,我得到了实际的 html 字符串。
最佳答案
您尝试获取的网址拒绝访问。
我更喜欢基于 Promise 的请求 API,所以下面的代码
var request = require("request");
request
.get("http://a810-bisweb.nyc.gov/bisweb/JobsQueryByNumberServlet?requestid=3&passjobnumber=123768556&passdocnumber=01")
.on('response', function(response) {
console.log('Hello');
console.log(response.statusCode);
console.log(response.headers['content-type']);
})
.on('error', function(error){
console.log(error);
})
将打印出来
Hello
403
text/html
我认为您收到 403 的原因是该网站可能设置了 cookie 或具有某些 session 状态,并且您将直接访问您想要的 url,而不是先点击首页。如果我直接访问 url,我也会在浏览器中得到 403,但是如果我先访问首页,然后访问 url,我就会得到该页面。
关于javascript - Node.js 请求在特定网站上不返回 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068599/