bloomfilterJAVA怎么弄爬虫

bloomfilter  时间:2021-07-01  阅读:()

JAVA怎么弄爬虫

以下是一个使用java实现的简单爬虫核心代码:   public void crawl() throws Throwable { while (continueCrawling()) { CrawlerUrl url = getNextUrl(); //获取待爬取队列中的下一个URL if (url != null) { printCrawlInfo(); String content = getContent(url); //获取URL的文本信息 //聚焦爬虫只爬取与主题内容相关的网页,这里采用正则匹配简单处理 if (isContentRelevant(content, this.regexpSearchPattern)) { saveContent(url, content); //保存网页至本地 //获取网页内容中的链接,并放入待爬取队列中 Collection urlStrings = extractUrls(content, url); addUrlsToUrlQueue(url, urlStrings); } else { System.out.println(url + " is not relevant ignoring ..."); } //延时防止被对方屏蔽 Thread.sleep(this.delayBetweenUrls); } } closeOutputStream(); } private CrawlerUrl getNextUrl() throws Throwable { CrawlerUrl nextUrl = null; while ((nextUrl == null) && (!urlQueue.isEmpty())) { CrawlerUrl crawlerUrl = this.urlQueue.remove(); //doWeHavePermissionToVisit:是否有权限访问该URL,友好的爬虫会根据网站提供的"Robot.txt"中配置的规则进行爬取 //isUrlAlreadyVisited:URL是否访问过,大型的搜索引擎往往采用BloomFilter进行排重,这里简单使用HashMap //eptable:是否达到指定的深度上限。

爬虫一般采取广度优先的方式。

一些网站会构建爬虫陷阱(自动生成一些无效链接使爬虫陷入死循环),采用深度限制加以避免 if (doWeHavePermissionToVisit(crawlerUrl) && (!isUrlAlreadyVisited(crawlerUrl)) && eptable(crawlerUrl)) { nextUrl = crawlerUrl; // System.out.println("Next url to be visited is " + nextUrl); } } return nextUrl; } private String getContent(CrawlerUrl url) throws Throwable { //HttpClient4.1的调用与之前的方式不同 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标记为已访问 markUrlAsVisited(url); return strBuf.toString(); } public static boolean isContentRelevant(String content, Pattern regexpPattern) { boolean retValue = false; if (content != null) { //是否符合正则表达式的条件 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(); } }

LOCVPS洛杉矶CN2线路KVM上线,洛杉矶/香港云地/香港邦联7折

LOCVPS发来了新的洛杉矶CN2线路主机上线通知,基于KVM架构,目前可与香港云地、香港邦联机房XEN架构主机一起适用7折优惠码,优惠后最低美国洛杉矶CN2线路KVM架构2GB内存套餐月付38.5元起。LOCPVS是一家成立较早的国人VPS服务商,目前提供洛杉矶MC、洛杉矶C3、和香港邦联、香港沙田电信、香港大埔、日本东京、日本大阪、新加坡、德国和荷兰等机房VPS主机,基于KVM或者XEN架构。...

GeorgeDatacenter:洛杉矶/达拉斯/芝加哥/纽约vps云服务器;2核/8GB/250GB/2TB流量/1Gbps端口,$84/年

georgedatacenter怎么样?GeorgeDatacenter是一家2017年成立的美国商家,正规注册公司(REG: 10327625611),其实是oneman。现在有优惠,有几款特价VPS,基于Vmware。支持Paypal付款。GeorgeDatacenter目前推出的一款美国vps,2核/8GB内存/250GB NVMe空间/2TB流量/1Gbps端口/Vmware/洛杉矶/达拉...

香港服务器多少钱一个月?香港云服务器最便宜价格

香港服务器多少钱一个月?香港服务器租用配置价格一个月多少,现在很多中小型企业在建站时都会租用香港服务器,租用香港服务器可以使网站访问更流畅、稳定性更好,安全性会更高等等。香港服务器的租用和其他地区的服务器租用配置元素都是一样的,那么为什么香港服务器那么受欢迎呢,香港云服务器最便宜价格多少钱一个月呢?阿里云轻量应用服务器最便宜的是1核1G峰值带宽30Mbps,24元/月,288元/年。不过我们一般选...

bloomfilter为你推荐
settimer如何使用SetTimer MFC 够详细电子日历我想做个项目 如何在电子日历中进行时间的选择arc是什么意思arctanx等于什么?民生电商民生电商与传统的电商有什么区别?smartupload为什么使用smartupload执行上传保存操作时用这句smart.save("upload")失败用smart.save("/upload")成功ruby语言Ruby是一种什么样的语言,主要用来做什么,现在有一家公司让我从java转ruby,我很犹豫?faq是什么意思在国际贸易中“FAQ”代表的是什么意思qq业务中心QQ业务办理桌面管理系统卸载桌面图标管理软件fences之后桌面无法显示了建立qq号怎样建一个QQ号
查询ip 花生壳免费域名 budgetvm 东莞电信局 t牌 permitrootlogin mobaxterm 服务器日志分析 512au 建站代码 大容量存储 京东商城双十一活动 免费mysql 赞助 泉州移动 河南移动网 福建铁通 双线asp空间 cxz 东莞服务器托管 更多