您的位置:

简单java爬虫,java爬虫程序

本文目录一览:

如何用Java写一个爬虫

import java.io.File;

import java.net.URL;

import java.net.URLConnection;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Scanner;

import java.util.UUID;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class DownMM {

public static void main(String[] args) throws Exception {

//out为输出的路径,注意要以\\结尾

String out = "D:\\JSP\\pic\\java\\";

try{

File f = new File(out);

if(! f.exists()) {

f.mkdirs();

}

}catch(Exception e){

System.out.println("no");

}

String url = "-";

Pattern reg = Pattern.compile("img src=\"(.*?)\"");

for(int j=0, i=1; i=10; i++){

URL uu = new URL(url+i);

URLConnection conn = uu.openConnection();

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");

Scanner sc = new Scanner(conn.getInputStream());

Matcher m = reg.matcher(sc.useDelimiter("\\A").next());

while(m.find()){

Files.copy(new URL(m.group(1)).openStream(), Paths.get(out + UUID.randomUUID() + ".jpg"));

System.out.println("已下载:"+j++);

}

}

}

}

Java网络爬虫怎么实现?

网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成。

传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL,在抓取网页的过程中,不断从当前页面上抽取新的URL放入队列,直到满足系统的一定停止条件。对于垂直搜索来说,聚焦爬虫,即有针对性地爬取特定主题网页的爬虫,更为适合。

以下是一个使用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

//isDepthAcceptable:是否达到指定的深度上限。爬虫一般采取广度优先的方式。一些网站会构建爬虫陷阱(自动生成一些无效链接使爬虫陷入死循环),采用深度限制加以避免

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的调用与之前的方式不同

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();

}

}

java适合写爬虫吗?

JAVA也可以实现爬虫,比如jsoup包,一个非常方便解析html的工具呢。

不过相对来说,java语言笨重,稍微有些麻烦。

简单java爬虫,java爬虫程序

2022-11-20
java爬虫(java爬虫和python爬虫)

2022-11-15
java爬虫,java爬虫代码

2023-01-09
爬虫java,爬虫java框架

2023-01-09
java爬虫,java爬虫与python爬虫的区别

2022-11-27
爬虫java,爬虫JavascriptvoidO

2022-12-02
java网络爬虫,爬虫 java

2023-01-06
java网络爬虫,爬虫Java

2022-11-29
jspider纯java爬虫(java实现爬虫)

本文目录一览: 1、在Java爬虫中使用Spider应该怎样初始化? 2、java 网络爬虫怎么实现 3、常用的java蜘蛛有哪些? 在Java爬虫中使用Spider应该怎样初始化? Java的属性初

2023-12-08
java爬虫系列第五讲(JAVA爬虫)

2022-11-12
爬虫pythonjson(爬虫python和java)

本文目录一览: 1、Python爬虫笔记(二)requests模块get,post,代理 2、Python爬虫(七)数据处理方法之JSON 3、Python与爬虫有什么关系? Python爬虫笔记(二

2023-12-08
一个简单的java爬虫框架(java爬虫框架排行)

2022-11-16
网络爬虫java,网络爬虫技术

2022-12-02
网络爬虫java,网络爬虫python代码

2023-01-06
java实现网页爬虫的示例讲解(java爬虫爬取网页内容)

2022-11-14
java并发小说爬虫,java爬取小说

2022-11-23
Java爬虫详解

2023-05-19
Java爬虫教程:从零开始实现爬虫程序

2023-05-17
java使用jsoup简单爬虫,jsoup爬取网页

本文目录一览: 1、java jsoup怎样爬取特定网页内的数据 2、java爬虫抓取指定数据 3、如何用java爬虫爬取招聘信息 java jsoup怎样爬取特定网页内的数据 1、Jsoup简述 J

2023-12-08
采集蛙java爬虫教学视频2的简单介绍

2022-11-16