1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
      常見(jiàn)爬蟲(chóng)java代碼,爬蟲(chóng)程序代碼

      跪求一份java網(wǎng)絡(luò)爬蟲(chóng)的源代碼急用!!

      希望能幫到你 . . . 這個(gè)可以解決你的問(wèn)題 但是沒(méi)有樣式的.只是爬了源代碼

      成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站與策劃設(shè)計(jì),秦州網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:秦州等地區(qū)。秦州做網(wǎng)站價(jià)格咨詢:18980820575

      import java.io.BufferedReader;

      import java.io.BufferedWriter;

      import java.io.File;

      import java.io.FileOutputStream;

      import java.io.InputStreamReader;

      import java.io.OutputStreamWriter;

      import java.net.HttpURLConnection;

      import java.net.URL;

      public class WebPageSource {

      public static void main(String args[]){

      URL url;

      int responsecode;

      HttpURLConnection urlConnection;

      BufferedReader reader;

      BufferedWriter writer;

      String line;

      try{

      //生成一個(gè)URL對(duì)象,要獲取源代碼的網(wǎng)頁(yè)地址為:

      url=new URL("");

      //打開(kāi)URL

      urlConnection = (HttpURLConnection)url.openConnection();

      //獲取服務(wù)器響應(yīng)代碼

      responsecode=urlConnection.getResponseCode();

      if(responsecode==200){

      //得到輸入流,即獲得了網(wǎng)頁(yè)的內(nèi)容

      reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"GBK"));

      writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("d://test.txt"))));

      while((line=reader.readLine())!=null){

      writer.write(line);

      writer.newLine();

      }

      }

      else{

      System.out.println("獲取不到網(wǎng)頁(yè)的源碼,服務(wù)器響應(yīng)代碼為:"+responsecode);

      }

      }

      catch(Exception e){

      System.out.println("獲取不到網(wǎng)頁(yè)的源碼,出現(xiàn)異常:"+e);

      }

      }

      }

      Java網(wǎng)絡(luò)爬蟲(chóng)怎么實(shí)現(xiàn)?

      網(wǎng)絡(luò)爬蟲(chóng)是一個(gè)自動(dòng)提取網(wǎng)頁(yè)的程序,它為搜索引擎從萬(wàn)維網(wǎng)上下載網(wǎng)頁(yè),是搜索引擎的重要組成。

      傳統(tǒng)爬蟲(chóng)從一個(gè)或若干初始網(wǎng)頁(yè)的URL開(kāi)始,獲得初始網(wǎng)頁(yè)上的URL,在抓取網(wǎng)頁(yè)的過(guò)程中,不斷從當(dāng)前頁(yè)面上抽取新的URL放入隊(duì)列,直到滿足系統(tǒng)的一定停止條件。對(duì)于垂直搜索來(lái)說(shuō),聚焦爬蟲(chóng),即有針對(duì)性地爬取特定主題網(wǎng)頁(yè)的爬蟲(chóng),更為適合。

      以下是一個(gè)使用java實(shí)現(xiàn)的簡(jiǎn)單爬蟲(chóng)核心代碼:

      public void crawl() throws Throwable {

      while (continueCrawling()) {

      CrawlerUrl url = getNextUrl(); //獲取待爬取隊(duì)列中的下一個(gè)URL

      if (url != null) {

      printCrawlInfo();

      String content = getContent(url); //獲取URL的文本信息

      //聚焦爬蟲(chóng)只爬取與主題內(nèi)容相關(guān)的網(wǎng)頁(yè),這里采用正則匹配簡(jiǎn)單處理

      if (isContentRelevant(content, this.regexpSearchPattern)) {

      saveContent(url, content); //保存網(wǎng)頁(yè)至本地

      //獲取網(wǎng)頁(yè)內(nèi)容中的鏈接,并放入待爬取隊(duì)列中

      Collection urlStrings = extractUrls(content, url);

      addUrlsToUrlQueue(url, urlStrings);

      } else {

      System.out.println(url + " is not relevant ignoring ...");

      }

      //延時(shí)防止被對(duì)方屏蔽

      Thread.sleep(this.delayBetweenUrls);

      }

      }

      closeOutputStream();

      }

      private CrawlerUrl getNextUrl() throws Throwable {

      CrawlerUrl nextUrl = null;

      while ((nextUrl == null) (!urlQueue.isEmpty())) {

      CrawlerUrl crawlerUrl = this.urlQueue.remove();

      //doWeHavePermissionToVisit:是否有權(quán)限訪問(wèn)該URL,友好的爬蟲(chóng)會(huì)根據(jù)網(wǎng)站提供的"Robot.txt"中配置的規(guī)則進(jìn)行爬取

      //isUrlAlreadyVisited:URL是否訪問(wèn)過(guò),大型的搜索引擎往往采用BloomFilter進(jìn)行排重,這里簡(jiǎn)單使用HashMap

      //isDepthAcceptable:是否達(dá)到指定的深度上限。爬蟲(chóng)一般采取廣度優(yōu)先的方式。一些網(wǎng)站會(huì)構(gòu)建爬蟲(chóng)陷阱(自動(dòng)生成一些無(wú)效鏈接使爬蟲(chóng)陷入死循環(huán)),采用深度限制加以避免

      if (doWeHavePermissionToVisit(crawlerUrl)

      (!isUrlAlreadyVisited(crawlerUrl))

      isDepthAcceptable(crawlerUrl)) {

      nextUrl = crawlerUrl;

      // System.out.println("Next url to be visited is " + nextUrl);

      }

      }

      return nextUrl;

      }

      private String getContent(CrawlerUrl url) throws Throwable {

      //HttpClient4.1的調(diào)用與之前的方式不同

      HttpClient client = new DefaultHttpClient();

      HttpGet httpGet = new HttpGet(url.getUrlString());

      StringBuffer strBuf = new StringBuffer();

      HttpResponse response = client.execute(httpGet);

      if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {

      HttpEntity entity = response.getEntity();

      if (entity != null) {

      BufferedReader reader = new BufferedReader(

      new InputStreamReader(entity.getContent(), "UTF-8"));

      String line = null;

      if (entity.getContentLength() 0) {

      strBuf = new StringBuffer((int) entity.getContentLength());

      while ((line = reader.readLine()) != null) {

      strBuf.append(line);

      }

      }

      }

      if (entity != null) {

      nsumeContent();

      }

      }

      //將url標(biāo)記為已訪問(wèn)

      markUrlAsVisited(url);

      return strBuf.toString();

      }

      public static boolean isContentRelevant(String content,

      Pattern regexpPattern) {

      boolean retValue = false;

      if (content != null) {

      //是否符合正則表達(dá)式的條件

      Matcher m = regexpPattern.matcher(content.toLowerCase());

      retValue = m.find();

      }

      return retValue;

      }

      public List extractUrls(String text, CrawlerUrl crawlerUrl) {

      Map urlMap = new HashMap();

      extractHttpUrls(urlMap, text);

      extractRelativeUrls(urlMap, text, crawlerUrl);

      return new ArrayList(urlMap.keySet());

      }

      private void extractHttpUrls(Map urlMap, String text) {

      Matcher m = (text);

      while (m.find()) {

      String url = m.group();

      String[] terms = url.split("a href=\"");

      for (String term : terms) {

      // System.out.println("Term = " + term);

      if (term.startsWith("http")) {

      int index = term.indexOf("\"");

      if (index 0) {

      term = term.substring(0, index);

      }

      urlMap.put(term, term);

      System.out.println("Hyperlink: " + term);

      }

      }

      }

      }

      private void extractRelativeUrls(Map urlMap, String text,

      CrawlerUrl crawlerUrl) {

      Matcher m = relativeRegexp.matcher(text);

      URL textURL = crawlerUrl.getURL();

      String host = textURL.getHost();

      while (m.find()) {

      String url = m.group();

      String[] terms = url.split("a href=\"");

      for (String term : terms) {

      if (term.startsWith("/")) {

      int index = term.indexOf("\"");

      if (index 0) {

      term = term.substring(0, index);

      }

      String s = //" + host + term;

      urlMap.put(s, s);

      System.out.println("Relative url: " + s);

      }

      }

      }

      }

      public static void main(String[] args) {

      try {

      String url = "";

      Queue urlQueue = new LinkedList();

      String regexp = "java";

      urlQueue.add(new CrawlerUrl(url, 0));

      NaiveCrawler crawler = new NaiveCrawler(urlQueue, 100, 5, 1000L,

      regexp);

      // boolean allowCrawl = crawler.areWeAllowedToVisit(url);

      // System.out.println("Allowed to crawl: " + url + " " +

      // allowCrawl);

      crawler.crawl();

      } catch (Throwable t) {

      System.out.println(t.toString());

      t.printStackTrace();

      }

      }

      如何用Java寫(xiě)一個(gè)爬蟲(chóng)

      下面說(shuō)明知乎爬蟲(chóng)的源碼和涉及主要技術(shù)點(diǎn):

      (1)程序package組織

      (2)模擬登錄(爬蟲(chóng)主要技術(shù)點(diǎn)1)

      要爬去需要登錄的網(wǎng)站數(shù)據(jù),模擬登錄是必要可少的一步,而且往往是難點(diǎn)。知乎爬蟲(chóng)的模擬登錄可以做一個(gè)很好的案例。要實(shí)現(xiàn)一個(gè)網(wǎng)站的模擬登錄,需要兩大步驟是:(1)對(duì)登錄的請(qǐng)求過(guò)程進(jìn)行分析,找到登錄的關(guān)鍵請(qǐng)求和步驟,分析工具可以有IE自帶(快捷鍵F12)、Fiddler、HttpWatcher;(2)編寫(xiě)代碼模擬登錄的過(guò)程。

      (3)網(wǎng)頁(yè)下載(爬蟲(chóng)主要技術(shù)點(diǎn)2)

      模擬登錄后,便可下載目標(biāo)網(wǎng)頁(yè)html了。知乎爬蟲(chóng)基于HttpClient寫(xiě)了一個(gè)網(wǎng)絡(luò)連接線程池,并且封裝了常用的get和post兩種網(wǎng)頁(yè)下載的方法。

      (4)自動(dòng)獲取網(wǎng)頁(yè)編碼(爬蟲(chóng)主要技術(shù)點(diǎn)3)

      自動(dòng)獲取網(wǎng)頁(yè)編碼是確保下載網(wǎng)頁(yè)html不出現(xiàn)亂碼的前提。知乎爬蟲(chóng)中提供方法可以解決絕大部分亂碼下載網(wǎng)頁(yè)亂碼問(wèn)題。

      (5)網(wǎng)頁(yè)解析和提取(爬蟲(chóng)主要技術(shù)點(diǎn)4)

      使用Java寫(xiě)爬蟲(chóng),常見(jiàn)的網(wǎng)頁(yè)解析和提取方法有兩種:利用開(kāi)源Jar包Jsoup和正則。一般來(lái)說(shuō),Jsoup就可以解決問(wèn)題,極少出現(xiàn)Jsoup不能解析和提取的情況。Jsoup強(qiáng)大功能,使得解析和提取異常簡(jiǎn)單。知乎爬蟲(chóng)采用的就是Jsoup。

      (6)正則匹配與提取(爬蟲(chóng)主要技術(shù)點(diǎn)5)

      雖然知乎爬蟲(chóng)采用Jsoup來(lái)進(jìn)行網(wǎng)頁(yè)解析,但是仍然封裝了正則匹配與提取數(shù)據(jù)的方法,因?yàn)檎齽t還可以做其他的事情,如在知乎爬蟲(chóng)中使用正則來(lái)進(jìn)行url地址的過(guò)濾和判斷。

      (7)數(shù)據(jù)去重(爬蟲(chóng)主要技術(shù)點(diǎn)6)

      對(duì)于爬蟲(chóng),根據(jù)場(chǎng)景不同,可以有不同的去重方案。(1)少量數(shù)據(jù),比如幾萬(wàn)或者十幾萬(wàn)條的情況,使用Map或Set便可;(2)中量數(shù)據(jù),比如幾百萬(wàn)或者上千萬(wàn),使用BloomFilter(著名的布隆過(guò)濾器)可以解決;(3)大量數(shù)據(jù),上億或者幾十億,Redis可以解決。知乎爬蟲(chóng)給出了BloomFilter的實(shí)現(xiàn),但是采用的Redis進(jìn)行去重。

      (8)設(shè)計(jì)模式等Java高級(jí)編程實(shí)踐

      除了以上爬蟲(chóng)主要的技術(shù)點(diǎn)之外,知乎爬蟲(chóng)的實(shí)現(xiàn)還涉及多種設(shè)計(jì)模式,主要有鏈模式、單例模式、組合模式等,同時(shí)還使用了Java反射。除了學(xué)習(xí)爬蟲(chóng)技術(shù),這對(duì)學(xué)習(xí)設(shè)計(jì)模式和Java反射機(jī)制也是一個(gè)不錯(cuò)的案例。

      4. 一些抓取結(jié)果展示

      用java編寫(xiě) 網(wǎng)絡(luò)爬蟲(chóng)求代碼和流程 急

      import java.awt.*;

      import java.awt.event.*;

      import java.io.*;

      import java.net.*;

      import java.util.*;

      import java.util.regex.*;

      import javax.swing.*;

      import javax.swing.table.*;//一個(gè)Web的爬行者(注:爬行在這里的意思與抓取,捕獲相同)

      public class SearchCrawler extends JFrame{

      //最大URL保存值

      private static final String[] MAX_URLS={"50","100","500","1000"};

      //緩存robot禁止爬行列表

      private HashMap disallowListCache=new HashMap();

      //搜索GUI控件

      private JTextField startTextField;

      private JComboBox maxComboBox;

      private JCheckBox limitCheckBox;

      private JTextField logTextField;

      private JTextField searchTextField;

      private JCheckBox caseCheckBox;

      private JButton searchButton;

      //搜索狀態(tài)GUI控件

      private JLabel crawlingLabel2;

      private JLabel crawledLabel2;

      private JLabel toCrawlLabel2;

      private JProgressBar progressBar;

      private JLabel matchesLabel2;

      //搜索匹配項(xiàng)表格列表

      private JTable table;

      //標(biāo)記爬行機(jī)器是否正在爬行

      private boolean crawling;

      //寫(xiě)日志匹配文件的引用

      private PrintWriter logFileWriter;

      //網(wǎng)絡(luò)爬行者的構(gòu)造函數(shù)

      public SearchCrawler(){

      //設(shè)置應(yīng)用程序標(biāo)題欄

      setTitle("搜索爬行者");

      //設(shè)置窗體大小

      setSize(600,600);

      //處理窗體關(guān)閉事件

      addWindowListener(new WindowAdapter(){

      public void windowClosing(WindowEvent e){

      actionExit();

      }

      });

      //設(shè)置文件菜單

      JMenuBar menuBar=new JMenuBar();

      JMenu fileMenu=new JMenu("文件");

      fileMenu.setMnemonic(KeyEvent.VK_F);

      JMenuItem fileExitMenuItem=new JMenuItem("退出",KeyEvent.VK_X);

      fileExitMenuItem.addActionListener(new ActionListener(){

      public void actionPerformed(ActionEvent e){

      actionExit();

      }

      });

      fileMenu.add(fileExitMenuItem);

      menuBar.add(fileMenu);

      setJMenuBar(menuBar);

      java爬蟲(chóng)抓取指定數(shù)據(jù)

      根據(jù)java網(wǎng)絡(luò)編程相關(guān)的內(nèi)容,使用jdk提供的相關(guān)類可以得到url對(duì)應(yīng)網(wǎng)頁(yè)的html頁(yè)面代碼。

      針對(duì)得到的html代碼,通過(guò)使用正則表達(dá)式即可得到我們想要的內(nèi)容。

      比如,我們?nèi)绻氲玫揭粋€(gè)網(wǎng)頁(yè)上所有包括“java”關(guān)鍵字的文本內(nèi)容,就可以逐行對(duì)網(wǎng)頁(yè)代碼進(jìn)行正則表達(dá)式的匹配。最后達(dá)到去除html標(biāo)簽和不相關(guān)的內(nèi)容,只得到包括“java”這個(gè)關(guān)鍵字的內(nèi)容的效果。

      從網(wǎng)頁(yè)上爬取圖片的流程和爬取內(nèi)容的流程基本相同,但是爬取圖片的步驟會(huì)多一步。

      需要先用img標(biāo)簽的正則表達(dá)式匹配獲取到img標(biāo)簽,再用src屬性的正則表達(dá)式獲取這個(gè)img標(biāo)簽中的src屬性的圖片url,然后再通過(guò)緩沖輸入流對(duì)象讀取到這個(gè)圖片url的圖片信息,配合文件輸出流將讀到的圖片信息寫(xiě)入到本地即可。


      當(dāng)前標(biāo)題:常見(jiàn)爬蟲(chóng)java代碼,爬蟲(chóng)程序代碼
      文章來(lái)源:http://www.ef60e0e.cn/article/dsisheh.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        那坡县| 芮城县| 裕民县| 龙里县| 德州市| 文安县| 吕梁市| 达日县| 铁岭县| 肇源县| 明水县| 政和县| 泾川县| 临江市| 彭泽县| 宁海县| 桦甸市| 清徐县| 嘉义市| 珠海市| 北安市| 武功县| 鄂伦春自治旗| 当涂县| 曲周县| 罗江县| 永新县| 长汀县| 德格县| 鄢陵县| 井研县| 铜陵市| 菏泽市| 会理县| 高尔夫| 庐江县| 东海县| 宜丰县| 大英县| 股票| 鄢陵县|