我通常使用Python开发程序,但是我想尝试最近进入Node JS并使用请求。我正在尝试向http://footlocker.com上的特定页面发送请求,该请求需要某些标头才能诱使该网站认为我正在使用常规浏览器访问该网站。在python中,我可以使用以下代码很好地发送请求并获得结果:
library(dplyr); library(purrr)
data <- tibble(col1 = letters[1:5], col2 = letters[6:10], col3 = letters[11:15])
f <- function(x){
if(is.numeric(x)) {
paste0(x, collapse = "")
} else {
print("Warning_by_print")
paste0(x, collapse = "")
}
}
result <- c()
captured_messages <- map(data, ~ capture.output(result <<- c(result, f(.x))))
# They are my desired output.
result; captured_unique_messages
# [1] "abcde" "fghij" "klmno"
# $col1
# [1] "[1] \"Warning_by_print\""
# ...
但是,当我尝试通过以下代码在Node JS中使用相同的标头发送相同的确切请求时:
import requests
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', 'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'accept-encoding':'gzip, deflate, br', 'accept-language':'en-US,en;q=0.9', 'cache-control':'max-age=0'}
r= requests.get('https://www.footlocker.com/api/products/pdp/19719100', headers=headers)
print(r.text)
我收到此错误:
const rp = require('request-promise');
var options = {
url: 'https://www.footlocker.com/api/products/pdp/19719100',
headers: {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36', 'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'accept-encoding':'gzip, deflate, br', 'accept-language':'en-US,en;q=0.9', 'cache-control':'max-age=0'
}
};
rp.get(options, function(err,resp,body){
console.log(resp);
console.log('Finished loading product!')
});
研究错误时,我发现该错误可能是由于网站阻止了我的请求,这很奇怪,因为我使用了正确的标头来防止这种情况在Python中起作用。
有人知道我在这里做错了吗,仅仅是我在Node JS中格式化我的请求格式有误还是其他问题?