美文网首页
http协议下的网络爬虫

http协议下的网络爬虫

作者: KingSun_阳 | 来源:发表于2015-12-10 09:33 被阅读0次

主管让做个抓取淘宝数据的功能,但是淘宝的比较难,我先从扒新浪新闻开始。

环境,Apache 提供免费的 HTTPClien t源码和 JAR 包下载,可以登陆这里下载,笔者用的是4.51版本。

参考apache提供的例子,使用正则表达式做出如下程序。


public class Main {
    
    public static void Detail(String url) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String oldStr;
        try {
            HttpGet httpget = new HttpGet(url);
            String encoding="gbk";
            if(url.contains("comments")){
                
                encoding = "utf-8";
            }
            System.out.println(encoding);
            System.out.println("Executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
          
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity entity = response.getEntity();
                oldStr = EntityUtils.toString(response.getEntity(),encoding);
 
                // Call abort on the request object
                httpget.abort();
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }

        Pattern pattern =  Pattern.compile("<title>[^<]*</title>");
        Matcher matcher = pattern.matcher(oldStr);
        if(matcher.find()){
            String str = matcher.group();
            str = str.substring(7,str.length()-8);
            System.out.println("---"+str);
        }
        
        pattern =  Pattern.compile("<p>[^<]*</p>");
        matcher = pattern.matcher(oldStr);
        while(matcher.find()){
            String str = matcher.group();
            str = str.substring(3,str.length()-4);
            System.out.println(str);
        }

    }

     

    
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String oldStr;
        try {
            
            String str = null;
        str ="http://news.sina.com.cn/hotnews/";
            HttpGet httpget = new HttpGet(str);
            System.out.println("Executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try { System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity entity = response.getEntity();
                oldStr = EntityUtils.toString(response.getEntity(),"UTF-8");
                // Call abort on the request object
                httpget.abort();
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
        Pattern pattern =  Pattern.compile("href='http://[^']*'");
        Matcher matcher = pattern.matcher(oldStr);
        int i= 1;
        while(matcher.find()){
            String str = matcher.group();
            str = str.substring(6,str.length()-1);
            System.out.println(str);
            Detail(str);
            System.out.println(i++);
        }
    }
}
```

相关文章

  • http协议下的网络爬虫

    主管让做个抓取淘宝数据的功能,但是淘宝的比较难,我先从扒新浪新闻开始。 环境,Apache 提供免费的 HTTPC...

  • 爬虫简介

    端口 | 网络协议 | 网络模型 | HTTP响应 端口 国际规定的通信协议(TCP/IP),爬虫使用的协议为HT...

  • Python库之网络爬虫

    Requests: 最友好的网络爬虫功能库-提供了简单易用的类HTTP协议网络爬虫功能-支持连接池,SSL, Co...

  • Robots协议

    好的网络爬虫,首先需要遵守Robots协议。Robots协议(也称为爬虫协议、机器人协议等)的全称是“网络爬虫排除...

  • Python爬虫基础教程之requests模块

    1、 引入 在学习爬虫之前可以先大致的了解一下HTTP协议~ HTTP协议:https://www.cnblogs...

  • Python 爬虫协议及建议

    爬虫协议 什么是爬虫协议:爬虫协议,也被叫做robots协议,是为了告诉网络蜘蛛哪些页面可以抓取,哪些页面不能抓取...

  • 爬虫的"盗亦有道"-Robots协议

    网络爬虫的君子协议 网络爬虫的尺寸 网络爬虫引发的问题 性能骚扰 法律风险 隐私泄露 网络爬虫的"性能骚扰"web...

  • 人生不得已——Python爬虫 robots协议

    关于robots协议 Robots协议(也称为爬虫协议、机器人协议等)的全称是“网络爬虫排除标准”(Robots ...

  • 关于爬虫

    关于爬虫 HTTP 协议 HTTP(Hypertext Transfer Protocol)是应用级协议,它适应了...

  • 爬虫入门系列(六):正则表达式完全指南(下)

    爬虫入门系列目录: 爬虫入门系列(一):快速理解HTTP协议 爬虫入门系列(二):优雅的HTTP库requests...

网友评论

      本文标题:http协议下的网络爬虫

      本文链接:https://www.haomeiwen.com/subject/kiqyhttx.html