【java爬虫】将优惠券数据存入数据库排序查询
发布人:shili8
发布时间:2024-12-26 18:39
阅读次数:0
**Java爬虫实践:优惠券数据存入数据库排序查询**
在本文中,我们将使用Java编程语言来实现一个简单的爬虫程序,用于抓取优惠券数据,并将其存储到数据库中。我们还会展示如何对这些数据进行排序和查询。
**环境准备**
* Java Development Kit (JDK)8或更高版本* MySQL数据库(本例中使用MySQL,但其他关系型数据库也可以)
* Apache Commons Lang库(用于字符串操作等功能)
### **爬虫程序设计**
我们的爬虫程序将从指定的网站抓取优惠券数据,然后将其存储到MySQL数据库中。我们还会实现对这些数据的排序和查询功能。
#### **爬虫类**
首先,我们定义一个名为`CouponCrawler`的类,负责抓取优惠券数据:
javapublic class CouponCrawler { private static final String URL = " //目标网站URL public Listcrawl() throws IOException { //使用HttpURLConnection或OkHttp库发送GET请求获取网页内容 HttpURLConnection connection = (HttpURLConnection) new URL(URL).openConnection(); connection.setRequestMethod("GET"); connection.connect(); //解析网页内容,提取优惠券数据 Document document = Jsoup.parse(connection.getInputStream(), "UTF-8", URL); Elements coupons = document.select("div.coupon"); List couponList = new ArrayList<>(); for (Element coupon : coupons) { Coupon couponData = parseCoupon(coupon); couponList.add(couponData); } return couponList; } private Coupon parseCoupon(Element element) { //解析单个优惠券元素,提取相关数据 String title = element.select("h2.title").text(); String description = element.select("p.description").text(); String discount = element.select("span.discount").text(); return new Coupon(title, description, discount); } }
#### **数据库类**
接下来,我们定义一个名为`CouponDatabase`的类,负责与MySQL数据库进行交互:
javapublic class CouponDatabase { private static final String DB_URL = "jdbc:mysql://localhost:3306/coupons"; //数据库连接URL private static final String USER = "root"; //数据库用户名 private static final String PASS = "password"; //数据库密码 public void saveCoupons(Listcoupons) { try (Connection connection = DriverManager.getConnection(DB_URL, USER, PASS); PreparedStatement statement = connection.prepareStatement("INSERT INTO coupons (title, description, discount) VALUES (?, ?, ?)")) { for (Coupon coupon : coupons) { statement.setString(1, coupon.getTitle()); statement.setString(2, coupon.getDescription()); statement.setString(3, coupon.getDiscount()); statement.executeUpdate(); } } catch (SQLException e) { System.err.println("Error saving coupons to database: " + e.getMessage()); } } public List queryCoupons() { try (Connection connection = DriverManager.getConnection(DB_URL, USER, PASS); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM coupons")) { List couponList = new ArrayList<>(); while (resultSet.next()) { Coupon couponData = parseCoupon(resultSet); couponList.add(couponData); } return couponList; } catch (SQLException e) { System.err.println("Error querying coupons from database: " + e.getMessage()); return Collections.emptyList(); } } private Coupon parseCoupon(ResultSet resultSet) throws SQLException { String title = resultSet.getString("title"); String description = resultSet.getString("description"); String discount = resultSet.getString("discount"); return new Coupon(title, description, discount); } }
#### **排序和查询**
最后,我们可以使用`CouponDatabase`类的方法对优惠券数据进行排序和查询:
javapublic class Main { public static void main(String[] args) { CouponCrawler crawler = new CouponCrawler(); Listcoupons = crawler.crawl(); CouponDatabase database = new CouponDatabase(); database.saveCoupons(coupons); List sortedCoupons = database.queryCoupons(); System.out.println("Sorted Coupons:"); for (Coupon coupon : sortedCoupons) { System.out.println(coupon.getTitle() + ": " + coupon.getDiscount()); } } }
在本文中,我们展示了如何使用Java编程语言实现一个简单的爬虫程序,用于抓取优惠券数据,并将其存储到MySQL数据库中。我们还展示了如何对这些数据进行排序和查询。