前言
爬虫神器,无代码爬取,就来:bright.cn
Java基本知识:
需要爬虫相关的PDF,并统计对应PDF里头的词频,其中某个功能需要如下知识点
1. 基本知识
Apache PDFBox 是一个开源的 Java PDF 操作库,支持:
读取 PDF 文件内容(包括文字、图片、元数据)
创建和修改 PDF 文档
提取文本内容用于搜索、分析等操作
Maven相关的依赖:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version>
</dependency>
需下载 在进行统计:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.File;
import java.io.IOException;
public class PDFWordCounter {
public static void main(String[] args) {
String pdfPath = "sample.pdf"; // 替换为你的 PDF 文件路径
String keyword = "Java"; // 要统计的词语
try {
// 加载 PDF 文档
PDDocument document = PDDocument.load(new File(pdfPath));
// 使用 PDFTextStripper 提取文本
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
document.close(); // 记得关闭文档资源
// 转小写处理,方便忽略大小写
String lowerText = text.toLowerCase();
String lowerKeyword = keyword.toLowerCase();
// 调用词频统计函数
int count = countOccurrences(lowerText, lowerKeyword);
System.out.println("词语 \"" + keyword + "\" 出现次数: " + count);
} catch (IOException e) {
e.printStackTrace();
}
}
// 使用 indexOf 遍历匹配词语出现次数
private static int countOccurrences(String text, String word) {
int count = 0;
int index = 0;
while ((index = text.indexOf(word, index)) != -1) {
count++;
index += word.length();
}
return count;
}
}
上述的Demo详细分析下核心知识:
PDDocument.load(File)
用于加载 PDF 文件到内存中
PDFBox 使用 PDDocument 表示整个 PDF 对象,使用完后必须调用 close() 释放资源PDFTextStripper
PDFBox 中用于提取文字的核心类,会尽可能“以阅读顺序”提取文本,适用于纯文字 PDF 文件。对于图像型扫描件则无效(需 OCR)大小写不敏感统计
实际应用中搜索关键词通常需要忽略大小写,因此我们先统一将文本和关键词转换为小写indexOf 实现词频统计
这是最基础也最直观的统计方法,效率较高,但不够精确
如果需要更精确(只统计完整单词),可以使用正则:
Pattern pattern = Pattern.compile("\\b" + Pattern.quote(word) + "\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
int count = 0;
while (matcher.find()) {
count++;
}
2. 在线URL
2.1 英文
此处的Demo需要注意一个点:
注意点 | 说明 |
---|---|
PDF 文件是否公开访问 | 不能访问受密码或登录保护的 PDF |
文件大小 | 不建议下载和分析过大文件,可能导致内存问题 |
中文 PDF | 若是扫描图片形式的中文 PDF,则 PDFBox 无法直接提取文本(需 OCR) |
编码问题 | 若中文显示为乱码,可能是 PDF 没有内嵌字体 |
🔧 思路:
通过 URL.openStream() 获取在线 PDF 的输入流
使用 PDFBox 的 PDDocument.load(InputStream) 读取 PDF
用 PDFTextStripper 提取文本
用字符串方法或正则统计关键词频率
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.InputStream;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OnlinePDFKeywordCounter {
public static void main(String[] args) {
String pdfUrl = "https://www.example.com/sample.pdf"; // 你的在线 PDF 链接
String keyword = "Java"; // 需要统计的关键词
try (InputStream inputStream = new URL(pdfUrl).openStream();
PDDocument document = PDDocument.load(inputStream)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// 使用正则匹配单词边界(忽略大小写)
Pattern pattern = Pattern.compile("\\b" + Pattern.quote(keyword) + "\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
int count = 0;
while (matcher.find()) {
count++;
}
System.out.println("词语 \"" + keyword + "\" 出现在在线 PDF 中的次数为: " + count);
} catch (Exception e) {
System.err.println("处理 PDF 时出错: " + e.getMessage());
e.printStackTrace();
}
}
}
2.2 混合
方法 | 适用场景 | 是否支持中文 |
---|---|---|
indexOf |
中英文都适用 | ✅ |
Pattern + \\b |
仅限英文单词匹配 | ❌ 中文不支持 |
正则表达式 \\b...\\b
(表示“单词边界”)并不适用于中文
统计在想的URL PDF的词频:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.InputStream;
import java.net.URL;
public class OnlinePDFKeywordCounter {
public static void main(String[] args) {
String pdfUrl = "https://www.xxxx.pdf";
String keyword = "管理层"; // 要统计的中文关键词
try (InputStream inputStream = new URL(pdfUrl).openStream();
PDDocument document = PDDocument.load(inputStream)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// 直接用 indexOf 不区分大小写(对于中文没必要转小写)
int count = countOccurrences(text, keyword);
System.out.println("词语 \"" + keyword + "\" 出现次数为: " + count);
} catch (Exception e) {
System.err.println("处理 PDF 时出错: " + e.getMessage());
e.printStackTrace();
}
}
// 简单统计子串出现次数(适用于中文)
private static int countOccurrences(String text, String keyword) {
int count = 0;
int index = 0;
while ((index = text.indexOf(keyword, index)) != -1) {
count++;
index += keyword.length();
}
return count;
}
}
截图如下:
3. 实战
如果词频比较多,可以使用List
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
public class OnlinePDFChinaKeywordCounter {
public static void main(String[] args) {
String pdfUrl = "https://www.pdf";
// 多个中文关键词
List<String> keywords = Arrays.asList("营业收入", "净利润", "资产总额", "负债");
try (InputStream inputStream = new URL(pdfUrl).openStream();
PDDocument document = PDDocument.load(inputStream)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// 统计 PDF 中的总文字长度(不含空格和换行)
int totalCharacters = text.replaceAll("\\s+", "").length();
System.out.println("PDF 中文本总字数(不含空格换行): " + totalCharacters);
for (String keyword : keywords) {
int count = countOccurrences(text, keyword);
System.out.println("词语 \"" + keyword + "\" 出现次数为: " + count);
}
} catch (Exception e) {
System.err.println("处理 PDF 时出错: " + e.getMessage());
e.printStackTrace();
}
}
// 统计某个关键词出现次数
private static int countOccurrences(String text, String keyword) {
int count = 0;
int index = 0;
while ((index = text.indexOf(keyword, index)) != -1) {
count++;
index += keyword.length();
}
return count;
}
}
截图如下: