java爬虫爬取网站数据实例
其他
2018-07-10 20:36:53
阅读次数: 0
002 |
package com.zzger.model;<font></font> |
004 |
import java.util.ArrayList;<font></font> |
005 |
import java.util.Collections;<font></font> |
006 |
import java.util.List;<font></font> |
007 |
import java.util.concurrent.CountDownLatch;<font></font> |
009 |
import com.zzger.module.queue.UrlQueue;<font></font> |
010 |
import com.zzger.util.HttpUtils;<font></font> |
011 |
import com.zzger.util.RegexUtils;<font></font> |
013 |
public class WebSite {<font></font> |
018 |
private String url;<font></font> |
021 |
* 需要爬行的url队列<font></font> |
023 |
private UrlQueue<String> urls = new UrlQueue<>();<font></font> |
026 |
* 已爬行过的页面url<font></font> |
028 |
private List<String> exitUrls = Collections.synchronizedList( new ArrayList<>());<font></font> |
030 |
private static final int TOTAL_THREADS = 12 ; <font></font> |
032 |
private final CountDownLatch mStartSignal = new CountDownLatch( 1 ); <font></font> |
034 |
private final CountDownLatch mDoneSignal = new CountDownLatch(TOTAL_THREADS); <font></font> |
036 |
public WebSite(String url){<font></font> |
037 |
this .url = url;<font></font> |
041 |
public void guangDu(){<font></font> |
042 |
new Thread( new Runnable() {<font></font> |
043 |
@Override <font></font> |
044 |
public void run() {<font></font> |
045 |
paxing(HttpUtils.httpGet(url));<font></font> |
047 |
}).start();<font></font> |
050 |
public void paxing(String html){<font></font> |
051 |
if (html.lastIndexOf( "下一页</a></li></ul></div>" )< 0 ) return ;<font></font> |
052 |
String strList = html.substring(html.indexOf( "<li class=\\" next-page\\ ">" ), <font></font> |
053 |
html.lastIndexOf( "下一页</a></li></ul></div>" ));<font></font> |
054 |
String url = RegexUtils.RegexString( "<a href=\\" (.+?)\\ "" , strList);<font></font> |
055 |
if (url.equals( "Nothing" )) return ;<font></font> |
057 |
paxing(HttpUtils.httpGet(url));<font></font> |
060 |
public void dxcPx(){<font></font> |
061 |
Page<DuanZi> page = new Gxpage(urls.take());<font></font> |
062 |
List<Section<DuanZi>> list = page.ybhqSection().getSections();<font></font> |
063 |
for (Section<DuanZi> section : list){<font></font> |
064 |
new Thread( new Runnable() {<font></font> |
065 |
@Override <font></font> |
066 |
public void run() {<font></font> |
067 |
mStartSignal.countDown(); |
069 |
mStartSignal.await(); |
071 |
} catch (InterruptedException e) {<font></font> |
072 |
e.printStackTrace();<font></font> |
074 |
DuanZi duanzi = section.select().getModel();<font></font> |
075 |
System.out.println(duanzi.getTitle());<font></font> |
076 |
mDoneSignal.countDown(); |
079 |
).start();<font></font> |
085 |
catch (InterruptedException e) <font></font> |
087 |
e.printStackTrace(); <font></font> |
088 |
} <font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
090 |
}</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
091 |
public static void main(String [] args){</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
092 |
WebSite web = new WebSite(“ https://www.bdqnhyq.com |
093 |
web.guangDu();</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
094 |
for ( int i = 0 ; i < 10 ; i ++){</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
095 |
新线程( new Runnable(){</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
096 |
@覆盖</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
097 |
public void run(){</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
098 |
web.dxcPx();</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
099 |
}</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
100 |
})。开始();</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
101 |
}</font></font><font></font> |
102 |
<font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
103 |
}</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
104 |
}</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > |
文章来源:
北大青鸟
开发小组
转载自blog.csdn.net/aide315/article/details/79569546