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

标签 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>

我无法理解恶意网址的原因

最佳答案

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

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

如果您删除 content-length ,它可以工作,但随着 url 的移动,您最终可能会收到 301 错误。因此您可能需要使用 follow-redirects 模块

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

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

关于javascript - 尝试从 YouTube 获取搜索结果时出现错误 "Your client has issued a malformed or illegal request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329426/

相关文章:

javascript - 如何使用 JQuery 读取远程文件(并获得 jqXHR 可读错误)

javascript - c# 限制在gridview中选择的复选框数量

node.js - heroku Node js简单跨域问题

http - 从 Web.py 中的 PUT 方法获取输入

XML SOAP 消息的 Java HTTP post

javascript - 如何在Javascript中将数字作为字符串创建关联数组

javascript - React 组件 this.variable 不渲染

mysql - 如何修复回调已经在环回模型中调用

node.js - Redis部署配置-主从复制

http - Meteor HTTP GET 在 Mongo 循环中崩溃