首页 > 其他 > 详细

跨域抓取360搜索结果

时间:2017-05-28 13:22:41      阅读:350      评论:0      收藏:0      [点我收藏+]

先来看效果,在搜索框中输入想要搜索的内容,信息就会显示在下方。

技术分享

 

如何把360搜索到的信息放入自己的网页,涉及到了跨域请求。

代码分析:

1.创建script元素.document.createElelment("script");

2.设置script的src,这个src即为360搜索的接口。script.src="https://sug.so.360.cn/suggest?callback=infoget&encodein=utf8&encodeout=utf8&format=json&fields=word&word="

其中callback为请求成功的回调函数,需要提前定义好。word=后跟着就想要请求的内容。

3.把这个script元素加入到document中。当把script加入到document时,就会去请求360的数据了。

4.返回的数据格式。

技术分享

主要是用到result,其他的在本次测试中没有用到。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="jquery-1.9.1.min.js"></script>
    <script>
        function infoget(data) {
           console.log(data.result[0].word);
        }
  
        $(function () {

 
        $("#btn").click(function () {
           
                var script = document.createElement("script");
                script.src = "https://sug.so.360.cn/suggest?callback=infoget&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=" + $("#txt").val();
                document.body.appendChild(script);
            });

        });
   
    </script>
</head>

<body>
    <input type="text" id="txt" style="width:500px">
    <input type="button" value="确定" id="btn">
    <div style="width:500px"></div>
</body>

</html>

 

跨域抓取360搜索结果

原文:http://www.cnblogs.com/xiaoai123/p/6915495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!