温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java之爬虫

发布时间:2020-06-15 23:09:54 来源:网络 阅读:1816 作者:水滴的历程 栏目:软件技术

近期研究爬虫爬取网站链接:
1.需要获取所有超链接
2.排除已爬取的链接,去重
3.爬虫的广度和深度方向研究(ps:目前没有研究彻底)
以下是实现代码:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.plaf.synth.SynthSpinnerUI;

public class study {
    private static List<String> waitforUrl=new ArrayList<>();//存储抓取出来的url,等待爬
    private static Set<String> goforUrl=new HashSet<>();//存储爬过的url
    private static Map<String,Integer> allUrldepth=new HashMap<>();//对所有url进行爬去深度判断
    private static int Maxdepth=2;
    public static void main(String[] args) {

        String urlstr="............";
        study.gourl(urlstr, 1);

    }

    public static void gourl(String urlstr,int depath) {
        if(!(goforUrl.contains(urlstr)||depath>Maxdepth)) {
            goforUrl.add(urlstr);
            try {
                URL url=new URL(urlstr);
                URLConnection urlConn=url.openConnection();//建立url链接
                InputStream is=urlConn.getInputStream();//通过链接获取页面内容,为字节流
                InputStreamReader isr=new InputStreamReader(is,"utf-8");//将字节流转化为字符流
                BufferedReader br=new BufferedReader(isr);//读取字节流

                StringBuffer sb=new StringBuffer();//实例化StringBuffer用来存储读取数据
                String line=null;

                while((line=br.readLine())!=null) {
                    sb.append(line);
                    //System.out.println(line);
                    Pattern p = Pattern.compile("<a .*href=.+</a>");
                    Matcher m=p.matcher(line);
                    while(m.find()) {
                        String href=m.group();
                        href=href.substring(href.indexOf("href="));
                        if(href.charAt(5)=='\"'||href.charAt(5)=='\''){
                            href=href.substring(6);
                          }else{
                            href=href.substring(5);
                          }
                        try {
                        href=href.substring(0,href.indexOf("\""));
                        }catch(Exception e) {
                            href=href.substring(0,href.indexOf("\'"));
                        }
                        waitforUrl.add(href);
                        allUrldepth.put(href, depath+1);
                    }
                }

                is.close();//关闭输出流
                isr.close();//关闭字节流读取
                br.close();
                System.out.println(urlstr);
                System.out.println("链接总数:"+waitforUrl.size()+",已爬去链接数:"+goforUrl.size());

            }catch(Exception e){
                 e.printStackTrace();
            }

        }
        //用递归的方法继续爬取其他链接
            String nexturl=waitforUrl.get(0);
            waitforUrl.remove(0);
            gourl(nexturl,allUrldepth.get(nexturl));
    }

}

java之爬虫
遇到的问题:allUrldepth作为hashmap,当key值即url一样时存在value值即depath被覆盖问题
解决办法:通过判断hashmap里key值是否包含,包含就把原有depath添加进去
if(allUrldepth.containsKey(href)) {
allUrldepth.put(href,depath);
}else {
allUrldepth.put(href, depath+1);
}

问题:通过一个线程执行速度太慢,通过多线程解决

解决办法:

private static int MAX_THREAD=5;//记录总线程数5条
private static Object obj=new Object();//Object对象,帮助进行线程的等待操作
public class myThread extends Thread{
        @Override
        public void run() 
        {
            while(true) 
            {
                if(waitforUrl.size()>0) 
                {
                    String url=waitforUrl.get(0);
                    waitforUrl.remove(0);
                    gourl(url, allUrldepth.get(url));
                }else 
                {
                    System.out.println("当前线程准备就绪,等待连接爬取:"+this.getName());
                      //建立一个对象,让线程进入等待状态,即wait()
                      synchronized(obj){
                        try{
                          obj.wait();
                        }catch(Exception e){

                        }

                }
            }
        }
    }

java之爬虫

问题:由于截取的部分a标签之间同时存在单双引号,例如www.baidu.com' ><img src="....</a>

解决办法:改变正则,只截取a标签前半部分,<a .*href=.+?>使用非贪婪模式获取

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI