在尝试从youtube获取搜索结果时出错“您的客户端发出了格式错误或非法的请求”

时间:2017-02-19 16:00:51

标签: javascript node.js http youtube

我正在尝试获取youtube搜索结果的页面。这里我输入了一个视频名称,并且我试图获取搜索结果的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>

我无法理解拼错网址的原因

1 个答案:

答案 0 :(得分:1)

检查您的内容长度行,这会导致格式错误的网址错误

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

如果你删除内容长度它可以工作,但你可能会在移动网址时获得301。所以你可能需要使用follow-redirects模块

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

或者你可以使用&#39; request&#39;模块