zoukankan      html  css  js  c++  java
  • JavaScript--模拟网络爬虫

     1 <!doctype html>
     2 <html>
     3  <head>
     4   <meta charset="UTF-8">
     5   <title>Document</title>
     6   <script>
     7     function getUrls(){
     8       var reg=
     9         /<as+[^>]*?href=['"]([^'"]+?)['"][^>]*?>/g;
    10       var arr=null;//声明变量arr,初始化为null
    11       //获取body元素的内容,保存在变量html中
    12       var html=document.body.innerHTML;
    13       //反复查找html中符合reg规则的字符串,保存在arr中,如果arr不等于null,就继续找
    14       while((arr=reg.exec(html))!=null){
    15         //arr: ["<a ....>","http://..."]
    16         //输出本次找到的a元素
    17         console.log(arr[1]);
    18                   //RegExp.$1 取出本次匹配的第一个分组的子内容
    19       }
    20     }
    21   </script>
    22  </head>
    23  <body>
    24   <link href="index.css"/><body><a class="header" href="http://tedu.cn">go to tedu</a><h1>welcome</h1><a name="top"></a><div>Hello World</div><a href="http://tmooc.cn" target="_blank">go to tmooc</a></body>
    25   <button onclick="getUrls()">开始爬虫</button>
    26  </body>
    27 </html>
  • 相关阅读:
    luogu 3388 【模板】割点(割顶)
    bzoj 3624 免费道路
    bzoj 1179 Atm
    bzoj 2428 均分数据
    luogu 4429 染色
    luogu 4427 求和
    luogu 1121 环状最大两段子段和
    hdu 4777 Queue
    hdu 5492 Find a path
    hdu 5441 Travel
  • 原文地址:https://www.cnblogs.com/chenzeyongjsj/p/5517499.html
Copyright © 2011-2022 走看看