【问题标题】:Error "Your client has issued a malformed or illegal request" while trying to get search results from youtube尝试从 youtube 获取搜索结果时出现错误“您的客户发出了格式错误或非法的请求”
【发布时间】:2017-02-19 16:00:50
【问题描述】:

我正在尝试获取 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>

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

【问题讨论】:

    标签: javascript node.js http youtube


    【解决方案1】:

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

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

    如果您删除内容长度它可以工作,但您最终可能会在 url 移动时得到 301。所以你可能需要使用跟随重定向模块

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

    或者你可以使用“请求”模块

    【讨论】:

    • 为什么我们会在删除内容长度标头时获得重定向?
    • 两者都是不同的问题 content-length 解决了您的格式错误的 url。重定向是因为您尝试使用 youtube.com (http) 已移动到 youtube.com (https)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2020-12-07
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多