尝试从YouTube获取搜索结果时出错"Your client has issued a malformed or illegal request"



我正在尝试获取YouTube的搜索结果的页面。在这里,我已经输入了一个视频名称,而IAM试图获取搜索结果的HTML页面。

我使用HTTP模块发送和收回数据。

var youtube_query=querystring.stringify({
  'search_query':'bangarang', //video name
  'spf':'navigate'
});
var options_you = {                     //headers for searching in youtube
  host:'www.youtube.com',
  path:'/results',
  method:'GET',
  headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(youtube_query),
      'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'

  }
};

function getvid_id(vid_result){  
                                              //callback function for finding the getting the result page
      vid_result.setEncoding('utf8');
      vid_result.on('data', function (chunk) {
     console.log(chunk);
    });      
}
var youtube_request = http.request(options_you,getvid_id);
youtube_request.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});
    youtube_request.write(youtube_query);
    youtube_request.end();

这是我收到的结果页

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-wi
dth">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{backgrou
nd:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height
:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/error
s/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflo
w:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (m
ax-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0
}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo
_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-reso
lution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/
2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:ur
l(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png)
0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:ur
l(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png)
no-repeat;-webkit-background-size:100%
100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>400.</b> <ins>ThatGÇÖs an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>ThatGÇÖs all w
e know.</ins>

iam无法理解失败的URL

的原因

检查您的内容长度行,这会导致错误的URL错误

'Content-Length': Buffer.byteLength(youtube_query)

如果您删除了内容长度,则可以在移动URL时最终获得301。因此,您可能需要使用后续重新指数模块

var http = require("follow-redirects").http;

或您可以使用"请求"模块

相关内容

最新更新