爬虫

package com.tanzhou.spiders;



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 
    * @ClassName: Main  
    * @Description: TODO(爬虫程序测试)  
    * @author Administrator  
    * @date 2018年4月30日  
    *
 */
public class Main {
    /**
     * @throws IOException 
     * 
        * @Title: processPage  
        * @Description: TODO(爬虫方法,用来从网页上爬取数据)  
        * @param @param URL    参数  
        * @return void    返回类型  
        * @throws
     */
    public static String processPage(String word,int x) throws IOException{
        
        List list = Ha.getList();
        
        Document doc = Jsoup.connect("https://www.baidu.com/s?wd="+word+"&pn="+x).get();
        
        Elements select2 = doc.select("head");
        int y = 1;
        if(x>0){
            y= x+1;
        }
        
        for(int i = y;i<(x+11);i++){
        
            Element elementById = doc.getElementById(""+i+"");
            list.add(elementById.toString());
            
        }
        String path = "D:/workspace/Spiders/WebContent/jsp/css.html";
        File f = new File(path);
        if (!f.exists()) {
            f.createNewFile();
        }
        Writer writer = new BufferedWriter(  
                new OutputStreamWriter(  
                        new FileOutputStream(f), "UTF-8"));  
        writer.write(select2.toString()); 
        for (int j = 0,len = list.size(); j < len; j++) {
             writer.append((CharSequence) list.get(j));
        }
        writer.flush();
        
        writer.close(); 
        return path;
    }
    public static void main(String[] args) throws IOException {
        
        processPage("haha",10);
    }
    
    
        

    
    
    }


    

package com.tanzhou.spiders;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




@WebServlet(urlPatterns= "/SpiderServlet")
public class SpiderServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String word = request.getParameter("word");
        String num = request.getParameter("num");
        int x = Integer.parseInt(num);
        x=10;
        String processPage =null;
        for(int i = 0;i<(x/10);i++){
            processPage = Main.processPage(word,i*10);
        }
        int of = processPage.indexOf("jsp");
        String substring = processPage.substring(of);
        
        System.out.println(substring);
        request.getRequestDispatcher("/"+substring).forward(request, response);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }
    
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 上一个文章讲解了Handler的基本使用,同时也有一些问题没有解决,本篇带你从源码的角度理解。首先让我们来看看Ha...
    coder安阅读 343评论 0 0
  • 没有名贵的单反,只有普通的手机。但是我觉得摄影的初衷就是为了记录美景,何必在意是用什么拍的呢?能够表达自己的心情就...
    蓝若惜蓝阅读 312评论 3 2
  • 心酸任水阔,分别泪沾巾。 凤凰没何处,梧桐空向人。 车行风尘远,日落夕阳春。 谁见南湖上,相思若青苹。 附刘长卿原...
    庭上望月阅读 429评论 4 12
  • 我以为我一个人就很好,想起你却使我倍感寂寞
    董十八阅读 234评论 0 0

友情链接更多精彩内容