序文:
インターネットの最初の波は今消えています。数千のデータに基づいたモノのインターネットの今後の時代により、データは企業の重要な戦略的リソースの1つになりました。データクロールテクノロジーに基づいて、この記事では、Java関連のクロールツールを紹介し、興味のある友人がテストできるデモソースコードを添付しています!
1)JDKには、ページまたはJSONを取得するためのHTTP接続が付属しています
2)JDKには、ページまたはJSONを取得するための独自のURL接続が付属しています
3)httpclientツールを取得し、ページまたはJSONを取得します
4)Commons-Ioツール、ページまたはJSONを取得
5)JSOUPツール(通常はHTMLフィールド解析に使用されます)、ページを取得し、JSONなしでフォーマットを返します】
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
完全なコード:
パッケージcom.yeezhao.common.http; import java.io.bufferedReader; Import java.io.inputStream; Impont java.io.inputStreamReader; Import java.net.httpurlconnection; Import Java.net.url; Import inmolg.commons.commons.commons.commons.comcomnons.commons.commons.commons.commons.htpclient.urient; org.apache.commons.httpclient.httpmethod; Import org.apache.commons.httpclient.methods.getMethod; Import org.apache.commons.io.ioutils; Import org.jsoup.jsoup;/** * http Tool比較httpfetchutil { / ** *アクセスステータスコードを取得 * @param request * @return * @throws例外 * / public static int getResponseCode(string request)throws exception {url url = new url(request); httpurlconnection conn =(httpurlconnection)url.openconnection(); conn.getResponseCode(); } / ** * 1)jdkには、ページまたはjson * @param charset * @return * @throws例外 * / public static string jdkfetch(string request、string charset)スロー{url url = new url(request); httpurlconnection conn =(httpurlconnection)url.openconnection(); //ブラウザパラメーターをmock conn.setRequestProperty( "user-agent"、 "mozilla/5.0(windows nt 6.1; wow64)applewebkit/537.36" + "(khtml、khtml、ycecko)chrome/45.0.2454.101 Safari/537.36"); if(conn.getResponseCode()== httpurlconnection.http_ok){inputstream input = conn.getInputStream(); stringbuffer sb = new StringBuffer(); BufferedReader Reader = new BufferedReader(new inputStreamReader(input、charset));文字列s; while((s = reader.readline())!= null){sb.append(s + "/n"); } input.close(); conn.disconnect(); return sb.tostring(); } 戻る ""; } / ** * 2)jdkには、ページまたはjson * @param charset * @return * @throws例外 * / public static string urlfetch(string request、string charset)スロー{url url = new url(request); ioutils.tostring(url.openstream())を返します。 } / ** * 3)httpclientツールを取得し、ページまたはjson * @param url * @param charset * @return * @throws例外 * / public static string httpclientfetch(string url、string charset)スロー例外{// get httpclient httpclient = new httpclient(); httpclient.getParams()。setContentCharset(charset); httpmethod method = new getMethod(url); httpclient.executemethod(method); return method.getResponseBodyAsstring(); } / ** * 4)commons-ioツール、ページまたはjson * @param url * @param charset * @return * @throws例外 * / public static string commonsiofetch(string url、string charset)スロー{return ioutils.tostring(new url(url)、charset); } / ** * 5)jSoupツール(通常はHTMLフィールド解析に使用)、ページを取得、非jsonリターンフォーマット * @param url * @throws例外 * / public static string jsoupfetch(string url)throws {return jsoup.parse(url(url)、2 * 1000).html()。 }}テストコード:
com.yeezhao.common.http httpfetchutiltest {string seeds [] = {"http://baike.baidu.com/view/1.htm","http://m.xmixalaya.com/tracks/26096131.json","http:// remyapi.yeezhao.com/api/query?wd=%E5%91%A8%E6%98%9FA9%A9%B0%E7%9A%84%E7%94%B5%E5%BD%B1 "};最終的な静的文字列default_charset = "utf-8"; @before public void setup()throws exception {} @after public void teardown()throws exception {system.out.println( "--- down ---"); } @test public void testgetResponseCode()スロー例外{for(string seed:seeds){int ressonsecode = httpfetchutil.getResponseCode(seed); system.out.println( "ret ="+responsecode); }} @test public void testjdkfetch()throws例外{for(string seed:seeds){string ret = httpfetchutil.jdkfetch(seed、default_charset); System.out.println( "ret ="+ret); }} @test public void testurlfetch()throws exception {for(string seed:seeds){string ret = httpfetchutil.urlfetch(seed、default_charset); System.out.println( "ret ="+ret); }} @test public void testhttpclientfetch()throws例外{for(string seeds){string ret = httpfetchutil.httpclientfetch(seed、default_charset); System.out.println( "ret ="+ret); }} @test public void testcommonsiofetch()throws exception {for(string seeds){string ret = httpfetchutil.commonsiofetch(seed、default_charset); System.out.println( "ret ="+ret); }} @test public void testjsoupfetch()throws exception {for(string seed:seeds){string ret = httpfetchutil.jsoupfetch(seed); System.out.println( "ret ="+ret); }}}添付ファイル:関連ジャーの依存関係
... <Dependency> groupId> org.jsoup </groupid> <artifactid> jsoup </artifactid> <version> 1.7.3 </version> </dependency> <dependency> commons-httpclient </groupId> <artifactid> comm ons-httpclient </artifactid> <バージョン> 3.1 </version> </dependency> <dependency> commons-io </groupid> <artifactid> commons-io </artifactid> <バージョン
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。