搜索
查看
编辑修改
首页
UNITY
NODEJS
PYTHON
AI
GIT
PHP
GO
CEF3
JAVA
HTML
CSS
搜索
笔触狂放9
这个屌丝很懒,什么也没留下!
关注作者
热门标签
jquery
HTML
CSS
PHP
ASP
PYTHON
GO
AI
C
C++
C#
PHOTOSHOP
UNITY
iOS
android
vue
xml
爬虫
SEO
LINUX
WINDOWS
JAVA
MFC
CEF3
CAD
NODEJS
GIT
Pyppeteer
article
热门文章
1
DALL-E 2: 基于CLIP的层级式文本生成图像模型_hierarchical text-conditional image generation wit
2
STM32 学习(系统滴答定时器)_systick->val
3
OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054
4
allure与pytest_allure pytest
5
计算机毕业设计PyFlink+Spark+Hive民宿推荐系统 酒店推荐系统 民宿酒店数据分析可视化大屏 民宿爬虫 民宿大数据 知识图谱 机器学习
6
css鼠标移入盒子添加border时发生抖动_css动态边框会抖动
7
【MySQL】-17 MySQL综合-3(MySQL创建数据库+MySQL查看数据库+MySQL修改数据库+MySQL删除数据库+MySQL选择数据库)_创建数据库以及查看数据库
8
python爬虫之selenium自动化操作
9
智能指针学习笔记_智能指针置空
10
深度学习与多模态数据融合:实践指南
当前位置:
article
> 正文
java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件_java如何爬取网站上的pdf
作者:笔触狂放9 | 2024-06-21 11:52:40
赞
踩
java如何爬取网站上的pdf
最近跟我同事一起做的项目要求读取txt,word,excel,ppt,pdf,html中的内容,不多说,先把代码贴出来,之后有时间再来做详细的解读。
这是读取txt文件
[html]
view plain
copy
/**
* 获取txt的文件内容 新建的默认格式 ,其它三种格式会乱码
*
* @param txtFile
* @return
*/
public String GetTxtContent(File txtFile) {
BufferedReader
reader
=
null
;
String
tempString
=
null
;
StringBuffer
contents
=
new
StringBuffer();
try {
reader
=
new
BufferedReader(new FileReader(txtFile));
while ((
tempString
=
reader
.readLine()) != null) {
contents.append(tempString);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return contents.toString().trim();
}
[html]
view plain
copy
<
h1
>
读取ppt
</
h1
>
/**
* 读取PPT的内容
*
* @param excleFile
* @return
*/
public String GetPPTContent(File excleFile) {
StringBuffer
contents
=
new
StringBuffer("");// 文档内容
InputStream
is
=
null
;
SlideShow
ppt
=
null
;
try {
is
=
new
FileInputStream(excleFile);
ppt
=
new
SlideShow(new HSLFSlideShow(is));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Slide[]
slides
=
ppt
.getSlides();
for (int
i
=
0
; i
<
slides.length
; i++) {
TextRun[]
t
=
slides
[i].getTextRuns();// 为了取得幻灯片的文字内容,建立TextRun
for (int
j
=
0
; j
<
t.length
; j++) {
contents.append(t[j].getText());// 这里会将文字内容加到content中去
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return contents.toString().trim();
}
[html]
view plain
copy
<
h1
>
读取excel
</
h1
>
/**
* 获取2007excle的内容
*
* @param exclexlsxFile
* @return
*/
public String GetExclexlsxContent(File exclexlsxFile) {
StringBuffer
content
=
null
;
XSSFWorkbook
workbook
=
null
;
InputStream
in
=
null
;
try {
in
=
new
FileInputStream(exclexlsxFile);
content
=
new
StringBuffer();
workbook
=
new
XSSFWorkbook(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int
numSheets
=
0
; numSheets
<
workbook.getNumberOfSheets
(); numSheets++) {
XSSFSheet
aSheet
=
workbook
.getSheetAt(numSheets);// 获得一个sheet
content.append("\n");
if (
null
== aSheet) {
continue;
}
for (int
rowNum
=
0
; rowNum
<
= aSheet.getLastRowNum(); rowNum++) {
content.append("\n");
XSSFRow
aRow
=
aSheet
.getRow(rowNum);
if (
null
== aRow) {
continue;
}
for (short
cellNum
=
0
; cellNum
<
= aRow.getLastCellNum(); cellNum++) {
XSSFCell
aCell
=
aRow
.getCell(cellNum);
if (
null
== aCell) {
continue;
}
if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
content.append(aCell.getRichStringCellValue()
.getString());
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
boolean
b
=
HSSFDateUtil
.isCellDateFormatted(aCell);
if (b) {
Date
date
=
aCell
.getDateCellValue();
SimpleDateFormat
df
=
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
content.append(df.format(date));
}
}
}
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString().trim();
}
/**
* 读取excle的内容
*
* @param excleFile
* @return
*/
public String GetExcleContent(File excleFile) {
StringBuffer
content
=
null
;
HSSFWorkbook
workbook
=
null
;
InputStream
in
=
null
;
try {
in
=
new
FileInputStream(excleFile);
content
=
new
StringBuffer();
workbook
=
new
HSSFWorkbook(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int
numSheets
=
0
; numSheets
<
workbook.getNumberOfSheets
(); numSheets++) {
HSSFSheet
aSheet
=
workbook
.getSheetAt(numSheets);// 获得一个sheet
content.append("\n");
if (
null
== aSheet) {
continue;
}
for (int
rowNum
=
0
; rowNum
<
= aSheet.getLastRowNum(); rowNum++) {
content.append("\n");
HSSFRow
aRow
=
aSheet
.getRow(rowNum);
if (
null
== aRow) {
continue;
}
for (int
cellNum
=
0
; cellNum
<
= aRow.getLastCellNum(); cellNum++) {
HSSFCell
aCell
=
aRow
.getCell(cellNum);
if (
null
== aCell) {
continue;
}
if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
content.append(aCell.getRichStringCellValue()
.getString());
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
boolean
b
=
HSSFDateUtil
.isCellDateFormatted(aCell);
if (b) {
Date
date
=
aCell
.getDateCellValue();
SimpleDateFormat
df
=
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
content.append(df.format(date));
}
}
}
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content.toString().trim();
}
[html]
view plain
copy
<
span
style
=
"font-size:48px;"
>
读取word
</
span
>
/**
* 获取word的内容
*
* @param wordPath
* 文件
* @return word的内容
*/
@SuppressWarnings("resource")
public String GetWordContent(File wordFile) {
String
strContent
=
""
;
FileInputStream
in
=
null
;
try {
in
=
new
FileInputStream(wordFile);
WordExtractor
text
=
new
WordExtractor(in);
strContent
=
text
.getText();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return strContent.trim();
}
/**
* 获取word2007的内容
*
* @param word2007Path
* @return
* @throws Exception
*/
public String GetWordDocxContent(File wordDocxFile) {
POIXMLTextExtractor extractor;
String
text2007
=
""
;
try {
OPCPackage
opcPackage
=
POIXMLDocument
.openPackage(wordDocxFile
.getPath());
extractor
=
new
XWPFWordExtractor(opcPackage);
text2007
=
extractor
.getText();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlException e) {
e.printStackTrace();
} catch (OpenXML4JException e) {
e.printStackTrace();
}
return text2007.trim();
}
[html]
view plain
copy
<
span
style
=
"font-size:48px;"
>
读取pdf
</
span
>
[html]
view plain
copy
/**
* 读取PDF文字的内容
*
* @param pdfPath
* pdf
* @return 返回pdf文件的内容
*/
public String GetPDFContent(File pdfFile) {
String
content
=
""
;
FileInputStream
is
=
null
;
PDDocument
doc
=
null
;
try {
is
=
new
FileInputStream(pdfFile);
PDFParser
parser
=
new
PDFParser(is);
parser.parse();
doc
=
parser
.getPDDocument();
PDFTextStripper
stripper
=
new
PDFTextStripper();
content
=
stripper
.getText(doc);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (doc != null) {
try {
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return content.trim();
}
[html]
view plain
copy
<
span
style
=
"font-size:48px;"
>
读取html
</
span
>
/**
* 读取网页纯文本内容用来存储索引方法*/
public String GetHTML(String url) throws ParserException{
Parser
parser
=
new
Parser(url);
StringBean
sb
=
new
StringBean();
//設置不需要頁面的鏈接信息
sb.setLinks(false);
//設置將不間斷空格由正規空格替代
sb.setReplaceNonBreakingSpaces(true);
//設置一系列空格由單一空格代替
sb.setCollapse(true);
parser.visitAllNodesWith(sb);
return sb.getStrings().trim();
}
/**@param filePath
* 文件上傳路徑
* 处理附件方法 获得JSON数组
* @throws Exception */
public String HandleFj(String param,IService service,String filePath) throws Exception{
JSONArray
json
=
null
;
ArrayList
<
IEntity
>
list
=
null
;
String
sql
=
""
;
String
fjtotalpath
=
""
;
try {
json
=
JSONArray
.fromObject(DataObject.getObjectValue("param"));
} catch (Exception e) {
e.printStackTrace();
return "";
}
if(!StringHelper.isNullOrEmpty(json)){
StringBuffer
fjcontenttotal
=
new
StringBuffer();
for(int
i
=
0
;i
<
json.length
();i++){
String
fileid
=
json
.getJSONObject(i).getString("id");//拿到fileid
String
name
=
json
.getJSONObject(i).getString("name");
if(!StringHelper.isNullOrEmpty(fileid)&&!StringHelper.isNullOrEmpty(name)){
sql
=
"select t.localpath from t_srffile t where t.file_id='"
+fileid+"'";
try {
list
=
service
.selectRaw(sql, null);
} catch (Exception e) {
e.printStackTrace();
}
for(IEntity o:list){
String
location
=
DataObject
.getStringValue(o.get("location"));
fjtotalpath
=
filePath
+location;
fjcontenttotal.append(this.GetFileContent(fjtotalpath));
}
}
return fjcontenttotal.toString();
}
}
return "";
}
声明:
本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:
https://www.wpsshop.cn/w/笔触狂放9/article/detail/743035
推荐阅读
article
oracle
blob
字段
转
,
java
oracle
blob
字段
的内容
转
成
字符
窜...
public static String ConvertBLOBtoString(String city_id, Str...
赞
踩
article
[
AIGC
]
Java
CompletableFuture
详解...
CompletableFuture
是java.util.concurrent包的一个类,该类实现了Future和Comp...
赞
踩
article
华为
OD机试C卷
--
找
城市
(
Java
& JS &
Python
& C
)
...
一张地图上有n个
城市
,
城市
和
城市
之间有且只有一条道路相连:要么直接相连,要么通过其它
城市
中转相连
(
可中转一次或多次
)
。城...
赞
踩
article
【2024最新华为OD-
C
/D卷试题汇总】[支持在
线
评测]
多段
线
路径
压缩(
100
分)- 三
语言
A
C
...
【2024最新华为OD-
C
/D卷试题汇总】[支持在
线
评测]
多段
线
路径
压缩(
100
分)- 三
语言
A
C
题解(
Python
/...
赞
踩
article
【2024最新
华为
OD-
C
/D卷试题汇总】[支持在线评测]
团队
派遣(
100
分) - 三
语言
A
C
题解...
K小姐是一家公司的人力资源经理,她需要从公司的 $n$ 名员工中选出一些人组成
团队
,参加一个比赛活动。每个员工都有一个能...
赞
踩
article
【2024最新
华为
OD-
C
/D卷试题汇总】[支持在线评测]
虚拟
理财
游戏
(
100
分) - 三语言
A
C
...
在一款
虚拟
游戏
中,玩家需要通过投资来增加自己的资产,以免被淘汰出局。
游戏
中有一家银行提供 $m$ 个
理财
产品,每个产品的...
赞
踩
article
【2024最新华为OD-
C
/D卷试题汇总】[支持在线评测]
密码
输入
检测
(
100
分) - 三
语言
A
C
...
K小姐最近在开发一个
密码
安全
检测
系统。系统会接收用户
输入
的
密码
字符串 $input$,其中字符 `【2024最新华为OD...
赞
踩
article
【2024最新华为OD-
C
/D卷试题汇总】[支持在线评测]
机场
航班
调度
程序
(
100
分) - 三语言...
在 【2024最新华为OD-
C
/D卷试题汇总】[支持在线评测]
机场
航班
调度
程序
(
100
分) - 三语言A
C
题解(Pyt...
赞
踩
article
【2024最新
华为
OD-
C
/D卷试题汇总】[支持在线评测]
连续
区间
和(
100
分) - 三
语言
A
C
题...
YA 在分析一组数据时,需要统计出有多少个
连续
区间
(包括单个元素)的和大于等于给定的阈值 $x$。现在他将这个任务交给你...
赞
踩
article
【2024最新
华为
OD-
C
/D卷试题汇总】[支持在线评测] K小姐
的
画图
机器
(
100
分) - 三
语言
...
【2024最新
华为
OD-
C
/D卷试题汇总】[支持在线评测] K小姐
的
画图
机器
(
100
分) - 三
语言
A
C
题解(Pytho...
赞
踩
article
【2024最新
华为
OD-
C
/D卷试题汇总】[支持在线评测]
伐木工
(200分) - 三
语言
A
C
题解
(...
【2024最新
华为
OD-
C
/D卷试题汇总】[支持在线评测]
伐木工
(200分) - 三
语言
A
C
题解
(
Python
/Jav...
赞
踩
article
import
java
.
io
.
io
except
io
n,
import
java
.
io
.IOExcept...
问题:[单选]
import
java
.
io
.IOExcept
io
n;public class Except
io
nTest...
赞
踩
article
HDFS
Java
API
上传
文件
_第3关:
hdfs
-
java
接口之
上传
文件
...
在
hdfs
目录下
上传
文件
(
上传
一个hadoop-2.7.7.tar.gz安装包)要求:(1)以
Java
API方式写代...
赞
踩
article
import
java
.io.*
;
出错问题...
今天用servelet技术编程,在学校机房成功了!可是为什么在自己的电脑却显示
import
java
.io.*
;
错误?...
赞
踩
article
[
AIGC
]
Java
List
接口
详解...
List
接口
是
Java
集合框架的一部分,它是一个有序的集合,允许包含重复的元素。
List
接口
在java.util包中,它...
赞
踩
article
【
职业
规划
】该如何
选择
职业
方向?性能?
自动化
?测开?学习
选择
python
、
java
?_技术
测试
和功能...
阅读目录前言
职业
规划
路线 市场需求
测试
行业分析
自动化
测试
性能
测试
安全
测试
测试
开发
测试
管理 如何
选择
java
...
赞
踩
article
JAVA
对接
Spring
-AI_
ollama
java
...
Ollama是一个开源的大型语言模型服务,提供了类似OpenAI的API接口和聊天界面,可以非常方便地部署最新版本的GP...
赞
踩
article
sqoop
导入
mysql
到
hbase
报错
java
.lang.NoSuchMethodError: ...
sqoop
导入
mysql
到
hbase
报错
java
.lang.NoSuchMethodError: org.
apache
...
赞
踩
article
hbase
jps
没有
hmaster
和
hregionsever
报
zookeeper
.Clien...
hbase
jps
没有
hmaster
和
hregionsever
查看日志发现在
hbase
-site.xml文件的 hbas...
赞
踩
article
pip
升级 报错
Requirement
already
satisfied
:
pip
in d:\w...
pip
升级 报错
Requirement
already
satisfied
:
pip
in d:\web\
html
\li...
赞
踩
相关标签
oracle blob字段转
AIGC
java
python
华为od
c语言
javascript
游戏
开发语言
算法