专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 java爬虫技术之如何使用Java制作网络爬虫?

java爬虫技术之如何使用Java制作网络爬虫?

更新时间:2020-06-22 11:59:49 来源:动力节点 浏览2279次

如何使用Java制作简单的Web爬网程序原型。制作Web搜寻器并不像听起来那样困难。只需按照指南进行操作,您将在1小时或更短的时间内迅速到达该地点,然后享受它可以为您提供的大量信息。由于这只是一个原型,因此您需要花费更多时间来根据需要自定义它。

java爬虫技术之如何使用Java制作网络爬虫?

以下是本教程的先决条件:

·基本Java程式设计

·关于SQL和MySQL数据库的一些知识。

如果您不想使用数据库,则可以使用文件来跟踪爬网历史记录。

1.目标

在本教程中,目标如下:

给定学校根URL,例如"mit.edu",返回包含该学校字符串"research"的所有页面

典型的搜寻器按以下步骤工作:

1.解析根网页("mit.edu"),并从该页面获取所有链接。要访问每个URL并解析HTML页面,我将使用JSoup,它是用Java编写的便捷的网页解析器。

2.使用从步骤1检索到的URL,并解析这些URL

3.执行上述步骤时,我们需要跟踪之前已处理过的页面,因此每个网页仅被处理一次。这就是我们需要数据库的原因。

2.设置MySQL数据库

如果您使用的是Ubuntu,则可以按照本指南安装Apache,MySQL,PHP和phpMyAdmin。

如果使用Windows,则只需使用WampServer。您可以简单地从wampserver.com下载它,并在一分钟内安装它,可以继续进行下一步。

我将使用phpMyAdmin来操作MySQL数据库。它只是使用MySQL的GUI界面。如果您使用任何其他工具或不使用GUI工具,那都很好。

3.创建一个数据库和一个表

创建一个名为"Crawler"的数据库,并创建一个名为"Record"的表,如下所示:

CREATE TABLE IF NOT EXISTS`Record`(
`RecordID`INT(11)NOT NULL AUTO_INCREMENT,
`URL`text NOT NULL,
PRIMARY KEY(`RecordID`))ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

4.开始使用Java进行爬网

1)下载JSoup核心库。

2)现在,在Jsoup中创建一个名为"Crawler"的项目,并将您下载的JSoup和mysql-connector jar文件添加到Java Build Path。(右键单击项目->选择"构建路径"->"配置构建路径"->单击"库"选项卡->单击"添加外部JAR")

3)创建一个名为"DB"的类,该类用于处理数据库操作。

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;

public class DB{
public Connection conn=null;
public DB(){
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/Crawler";
conn=DriverManager.getConnection(url,"root","admin213");
System.out.println("conn built");
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}

public ResultSet runSql(String sql)throws SQLException{
Statement sta=conn.createStatement();
return sta.executeQuery(sql);
}

public boolean runSql2(String sql)throws SQLException{
Statement sta=conn.createStatement();
return sta.execute(sql);
}

Override
protected void finalize()throws Throwable{
if(conn!=null||!conn.isClosed()){
conn.close();
}
}}

4)创建一个名称为"Main"的类,它将作为我们的搜寻器。

import java.io.IOException;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;

public class Main{
public static DB db=new DB();
public static void main(String[]args)throws SQLException,IOException{
db.runSql2("TRUNCATE Record;");
processPage("http://www.mit.edu");
}
public static void processPage(String URL)throws SQLException,IOException{
//check if the given URL is already in database
String sql="select*from Record where URL='"+URL+"'";
ResultSet rs=db.runSql(sql);
if(rs.next()){
}else{
//store the URL to database to avoid parsing again
sql="INSERT INTO`Crawler`.`Record`"+"(`URL`)VALUES"+"(?);";
PreparedStatement stmt=db.conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
stmt.setString(1,URL);
stmt.execute();
//get useful information
Document doc=Jsoup.connect("http://www.mit.edu/").get();
if(doc.text().contains("research")){
System.out.println(URL);
}

//get all links and recursively call the processPage method
Elements questions=doc.select("a[href]");
for(Element link:questions){
if(link.attr("href").contains("mit.edu"))
processPage(link.attr("abs:href"));
}
}
}}

以上就是动力节点java培训机构的小编针对“java爬虫技术之如何使用Java制作网络爬虫?”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>