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 {
【网页爬虫java代码 java爬取网页内容】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 = https://www.04ip.com/post/false;
if (content != null) {
//是否符合正则表达式的条件
Matcher m = regexpPattern.matcher(content.toLowerCase());
retValue = https://www.04ip.com/post/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=https://www.04ip.com/"");
for (String term : terms) {
// System.out.println("Term = "term);
if (term.startsWith("http")) {
int index = term.indexOf("\"");
if (index0) {
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=https://www.04ip.com/"");
for (String term : terms) {
if (term.startsWith("/")) {
int index = term.indexOf("\"");
if (index0) {
term = term.substring(0, index);
}
String s = //"hostterm;
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爬虫爬取招聘信息1、思路:
明确需要爬取的信息
分析网页结构
分析爬取流程
优化
2、明确需要爬取的信息
职位名称
工资
职位描述
公司名称
公司主页
详情网页
分析网页结构
3、目标网站-拉勾网
网站使用json作为交互数据,分析json数据 , 需要的json关键数据
查看需要的信息所在的位置 , 使用Jsoup来解析网页
4、分析爬取流程
1.获取所有的positionId生成详情页,存放在一个存放网址列表中ListString joburls
2.获取每个详情页并解析为Job类,得到一个存放Job类的列表ListJob jobList
3.把ListJob jobList存进Excel表格中
Java操作Excel需要用到jxl
5、关键代码实现
public ListString getJobUrls(String gj,String city,String kd){
String pre_url="";
String end_url=".html";
String url;
if (gj.equals("")){
url=";city=" city "needAddtionalResult=falsefirst=falsepn=" pn "kd=" kd;
}else {
url="" gj "px=defaultcity=" city "needAddtionalResult=falsefirst=falsepn=" pn "kd=" kd;
}
String rs=getJson(url);
System.out.println(rs);
int total= JsonPath.read(rs,"$.content.positionResult.totalCount");//获取总数
int pagesize=total/15;
if (pagesize=30){
pagesize=30;
}
System.out.println(total);
// System.out.println(rs);
ListInteger posid=JsonPath.read(rs,"$.content.positionResult.result[*].positionId");//获取网页id
for (int j=1;j=pagesize;j){//获取所有的网页id
pn;//更新页数
url="" gj "px=defaultcity=" city "needAddtionalResult=falsefirst=falsepn=" pn "kd=" kd;
String rs2=getJson(url);
ListInteger posid2=JsonPath.read(rs2,"$.content.positionResult.result[*].positionId");
posid.addAll(posid2); //添加解析的id到第一个list
}
ListString joburls=new ArrayList();
//生成网页列表
for (int id:posid){
String url3=pre_url id end_url;
joburls.add(url3);
}
return joburls;
}
public Job getJob(String url){//获取工作信息
Job job=new Job();
Document document= null;
document = Jsoup.parse(getJson(url));
job.setJobname(document.select(".name").text());
job.setSalary(document.select(".salary").text());
String joball=HtmlTool.tag(document.select(".job_bt").select("div").html());//清除html标签
job.setJobdesc(joball);//职位描述包含要求
job.setCompany(document.select(".b2").attr("alt"));
Elements elements=document.select(".c_feature");
//System.out.println(document.select(".name").text());
job.setCompanysite(elements.select("a").attr("href")); //获取公司主页
job.setJobdsite(url);
return job;
}
void insertExcel(ListJob jobList) throws IOException, BiffException, WriteException {
int row=1;
Workbook wb = Workbook.getWorkbook(new File(JobCondition.filename));
WritableWorkbook book = Workbook.createWorkbook(new File(JobCondition.filename), wb);
WritableSheet sheet=book.getSheet(0);
for (int i=0;ijobList.size();i){//遍历工作列表,一行行插入到表格中
sheet.addCell(new Label(0,row,jobList.get(i).getJobname()));
sheet.addCell(new Label(1,row,jobList.get(i).getSalary()));
sheet.addCell(new Label(2,row,jobList.get(i).getJobdesc()));
sheet.addCell(new Label(3,row,jobList.get(i).getCompany()));
sheet.addCell(new Label(4,row,jobList.get(i).getCompanysite()));
sheet.addCell(new Label(5,row,jobList.get(i).getJobdsite()));
row;
}
book.write();
book.close();
}
Java网络爬虫怎么实现?网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成 。\x0d\x0a传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL , 在抓取网页的过程中,不断从当前页面上抽取新的URL放入队列,直到满足系统的一定停止条件 。对于垂直搜索来说,聚焦爬虫,即有针对性地爬取特定主题网页的爬虫,更为适合 。\x0d\x0a\x0d\x0a以下是一个使用java实现的简单爬虫核心代码:\x0d\x0apublic void crawl() throws Throwable {\x0d\x0awhile (continueCrawling()) {\x0d\x0aCrawlerUrl url = getNextUrl(); //获取待爬取队列中的下一个URL\x0d\x0aif (url != null) {\x0d\x0aprintCrawlInfo();\x0d\x0aString content = getContent(url); //获取URL的文本信息\x0d\x0a\x0d\x0a//聚焦爬虫只爬取与主题内容相关的网页,这里采用正则匹配简单处理\x0d\x0aif (isContentRelevant(content, this.regexpSearchPattern)) {\x0d\x0asaveContent(url, content); //保存网页至本地\x0d\x0a\x0d\x0a//获取网页内容中的链接,并放入待爬取队列中\x0d\x0aCollection urlStrings = extractUrls(content, url);\x0d\x0aaddUrlsToUrlQueue(url, urlStrings);\x0d\x0a} else {\x0d\x0aSystem.out.println(url" is not relevant ignoring ...");\x0d\x0a}\x0d\x0a\x0d\x0a//延时防止被对方屏蔽\x0d\x0aThread.sleep(this.delayBetweenUrls);\x0d\x0a}\x0d\x0a}\x0d\x0acloseOutputStream();\x0d\x0a}\x0d\x0aprivate CrawlerUrl getNextUrl() throws Throwable {\x0d\x0aCrawlerUrl nextUrl = null;\x0d\x0awhile ((nextUrl == null)(!urlQueue.isEmpty())) {\x0d\x0aCrawlerUrl crawlerUrl = this.urlQueue.remove();\x0d\x0a//doWeHavePermissionToVisit:是否有权限访问该URL,友好的爬虫会根据网站提供的"Robot.txt"中配置的规则进行爬取\x0d\x0a//isUrlAlreadyVisited:URL是否访问过,大型的搜索引擎往往采用BloomFilter进行排重,这里简单使用HashMap\x0d\x0a//isDepthAcceptable:是否达到指定的深度上限 。爬虫一般采取广度优先的方式 。一些网站会构建爬虫陷阱(自动生成一些无效链接使爬虫陷入死循环),采用深度限制加以避免\x0d\x0aif (doWeHavePermissionToVisit(crawlerUrl)\x0d\x0a(!isUrlAlreadyVisited(crawlerUrl))\x0d\x0aisDepthAcceptable(crawlerUrl)) {\x0d\x0anextUrl = crawlerUrl;\x0d\x0a// System.out.println("Next url to be visited is "nextUrl);\x0d\x0a}\x0d\x0a}\x0d\x0areturn nextUrl;\x0d\x0a}\x0d\x0aprivate String getContent(CrawlerUrl url) throws Throwable {\x0d\x0a//HttpClient4.1的调用与之前的方式不同\x0d\x0aHttpClient client = new DefaultHttpClient();\x0d\x0aHttpGet httpGet = new HttpGet(url.getUrlString());\x0d\x0aStringBuffer strBuf = new StringBuffer();\x0d\x0aHttpResponse response = client.execute(httpGet);\x0d\x0aif (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {\x0d\x0aHttpEntity entity = response.getEntity();\x0d\x0aif (entity != null) {\x0d\x0aBufferedReader reader = new BufferedReader(\x0d\x0anew InputStreamReader(entity.getContent(), "UTF-8"));\x0d\x0aString line = null;\x0d\x0aif (entity.getContentLength()0) {\x0d\x0astrBuf = new StringBuffer((int) entity.getContentLength());\x0d\x0awhile ((line = reader.readLine()) != null) {\x0d\x0astrBuf.append(line);\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0aif (entity != null) {\x0d\x0ansumeContent();\x0d\x0a}\x0d\x0a}\x0d\x0a//将url标记为已访问\x0d\x0amarkUrlAsVisited(url);\x0d\x0areturn strBuf.toString();\x0d\x0a}\x0d\x0apublic static boolean isContentRelevant(String content,\x0d\x0aPattern regexpPattern) {\x0d\x0aboolean retValue = https://www.04ip.com/post/false;/x0d/x0aif (content != null) {/x0d/x0a//是否符合正则表达式的条件/x0d/x0aMatcher m = regexpPattern.matcher(content.toLowerCase());/x0d/x0aretValue = m.find();/x0d/x0a}/x0d/x0areturn retValue;/x0d/x0a}/x0d/x0apublic List extractUrls(String text, CrawlerUrl crawlerUrl) {/x0d/x0aMap urlMap = new HashMap();/x0d/x0aextractHttpUrls(urlMap, text);/x0d/x0aextractRelativeUrls(urlMap, text, crawlerUrl);/x0d/x0areturn new ArrayList(urlMap.keySet());/x0d/x0a}/x0d/x0aprivate void extractHttpUrls(Map urlMap, String text) {/x0d/x0aMatcher m = (text);/x0d/x0awhile (m.find()) {/x0d/x0aString url = m.group();/x0d/x0aString[] terms = url.split("a href=https://www.04ip.com/"");\x0d\x0afor (String term : terms) {\x0d\x0a// System.out.println("Term = "term);\x0d\x0aif (term.startsWith("http")) {\x0d\x0aint index = term.indexOf("\"");\x0d\x0aif (index0) {\x0d\x0aterm = term.substring(0, index);\x0d\x0a}\x0d\x0aurlMap.put(term, term);\x0d\x0aSystem.out.println("Hyperlink: "term);\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0aprivate void extractRelativeUrls(Map urlMap, String text,\x0d\x0aCrawlerUrl crawlerUrl) {\x0d\x0aMatcher m = relativeRegexp.matcher(text);\x0d\x0aURL textURL = crawlerUrl.getURL();\x0d\x0aString host = textURL.getHost();\x0d\x0awhile (m.find()) {\x0d\x0aString url = m.group();\x0d\x0aString[] terms = url.split("a href=https://www.04ip.com/"");\x0d\x0afor (String term : terms) {\x0d\x0aif (term.startsWith("/")) {\x0d\x0aint index = term.indexOf("\"");\x0d\x0aif (index0) {\x0d\x0aterm = term.substring(0, index);\x0d\x0a}\x0d\x0aString s = //"hostterm;\x0d\x0aurlMap.put(s, s);\x0d\x0aSystem.out.println("Relative url: "s);\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0apublic static void main(String[] args) {\x0d\x0atry {\x0d\x0aString url = "";\x0d\x0aQueue urlQueue = new LinkedList();\x0d\x0aString regexp = "java";\x0d\x0aurlQueue.add(new CrawlerUrl(url, 0));\x0d\x0aNaiveCrawler crawler = new NaiveCrawler(urlQueue, 100, 5, 1000L,\x0d\x0aregexp);\x0d\x0a// boolean allowCrawl = crawler.areWeAllowedToVisit(url);\x0d\x0a// System.out.println("Allowed to crawl: "url" "\x0d\x0a// allowCrawl);\x0d\x0acrawler.crawl();\x0d\x0a} catch (Throwable t) {\x0d\x0aSystem.out.println(t.toString());\x0d\x0at.printStackTrace();\x0d\x0a}\x0d\x0a}
用java编写 网络爬虫求代码和流程 急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.*;//一个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;
//搜索状态GUI控件
private JLabel crawlingLabel2;
private JLabel crawledLabel2;
private JLabel toCrawlLabel2;
private JProgressBar progressBar;
private JLabel matchesLabel2;
//搜索匹配项表格列表
private JTable table;
//标记爬行机器是否正在爬行
private boolean crawling;
//写日志匹配文件的引用
private PrintWriter logFileWriter;
//网络爬行者的构造函数
public SearchCrawler(){
//设置应用程序标题栏
setTitle("搜索爬行者");
//设置窗体大小
setSize(600,600);
//处理窗体关闭事件
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
actionExit();
}
});
//设置文件菜单
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代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java爬取网页内容、网页爬虫java代码的信息别忘了在本站进行查找喔 。
推荐阅读
- redis缓存mysql数据同步的简单介绍
- 憨豆先生解谜游戏下载,憨豆先生手机游戏
- 快手直播怎么开同城推荐,快手直播间怎么上同城
- vb.net显示窗体2的简单介绍
- sqlserver集合查询,sql查询并集
- java版魂斗罗源代码,python魂斗罗源码
- 直播间小奶狗找对象文案,直播找对象标题怎么写
- oracle伪列怎么写 oracle虚表和伪列
- 荣耀什么手机升级鸿蒙系统,荣耀什么手机升级鸿蒙系统最好