爬虫小例子

Posted 小祥工作室

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬虫小例子相关的知识,希望对你有一定的参考价值。

package com.textPa.two;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class RetrivePage {
    
    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
//        HttpGet getHttp = new HttpGet("http://www.baidu.com");
        HttpGet getHttp = new HttpGet("http://club.news.sohu.com/zz0578/thread/4bqnexpi3no");
        String content = null;
        BufferedWriter writer = null;
        
        HttpResponse response;
        try {
            response = httpClient.execute(getHttp);
            HttpEntity entity = response.getEntity();
            
            if(entity!=null){
                content = EntityUtils.toString(entity,Charset.forName("GBK"));
                System.out.println(content);
                File file = new File("d:\\baidu.html");
                writer = new BufferedWriter(new FileWriter(file));
                writer.write(content);
                writer.flush();
                writer.close();
                System.out.println("创建成功");
            }
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
    
}

所需要的两个jar包我后面会贴出来

http://pan.baidu.com/s/1nuFuDUL

以上是关于爬虫小例子的主要内容,如果未能解决你的问题,请参考以下文章

VS中添加自定义代码片段——偷懒小技巧

爬虫小例子

使用正则做的一个爬虫小例子

scrapy按顺序启动多个爬虫代码片段(python3)

第一个用python3写的爬虫小例子

scrapy主动退出爬虫的代码片段(python3)