我有如下的HTML电子邮件体。
我想获取emailBody中存在的每个元素(公司、优先级、描述等),最后生成Json键值对。
我希望通过使用JSOUP库我们能够实现这一点。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none">
<!--
p
{margin-top:0;
margin-bottom:0}
-->
</style>
</head>
<body dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<a>
<b>
<span lang="EN-US" style="font-size:10.0pt; font-family:"Segoe UI",sans-serif; color:black">Mandatory fields are Ticket Type, Company, Priority, Short Description. </span>
</b>
</a>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<span lang="EN-US" style=""> </span>
</b>
</span>
</p>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<span style=""></span>
<table class="MsoNormalTable" align="left" width="666" style="width:499.25pt; border-collapse:collapse; border:none; margin-left:6.75pt; margin-right:6.75pt; margin-bottom:5.75pt">
<tbody>
<tr style="height:19.85pt">
<td width="150" valign="top" style="width:112.25pt; border:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt; height:19.85pt">
<p class="MsoNormal" align="right" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; text-align:right; line-height:105%">
<span style=""></span>
<span style="">
<b>
<span style="color:red">Ticket Type: </span>
</b>
</span>
<span style="">
<span style=""> </span>
</span>
</p>
</td>
<td width="516" valign="top" style="width:387.0pt; border:solid windowtext 1.0pt; border-left:none; padding:0cm 5.4pt 0cm 5.4pt; height:19.85pt">
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; line-height:105%">
<span style="">
<span style="">Incident </span>
</span>
</p>
</td>
</tr>
<tr style="height:17.35pt">
<td width="150" valign="top" style="width:112.25pt; border:solid windowtext 1.0pt; border-top:none; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<p class="MsoNormal" align="right" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; text-align:right; line-height:105%">
<span style="">
<b>
<span style="">
<span style="color:red">Company:</span>
</span>
</b>
</span>
</p>
</td>
<td width="516" valign="top" style="width:387.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; line-height:105%">
<span style="">
<span style="">Grupo Bimbo DSDE </span>
</span>
</p>
</td>
</tr>
<tr style="height:17.35pt">
<td width="150" valign="top" style="width:112.25pt; border:solid windowtext 1.0pt; border-top:none; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<p class="MsoNormal" align="right" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; text-align:right; line-height:105%">
<span style="">
<b>
<span style="">Configuration Item: </span>
</b>
</span>
</p>
</td>
<td width="516" valign="top" style="width:387.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<span style=""></span>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; line-height:105%">
<span style="">
<span style=""> </span>
</span>
</p>
</td>
</tr>
<tr style="height:17.35pt">
<td width="150" valign="top" style="width:112.25pt; border:solid windowtext 1.0pt; border-top:none; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<p class="MsoNormal" align="right" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; text-align:right; line-height:105%">
<span style="">
<b>
<span style="color:red">Priority: </span>
</b>
</span>
</p>
</td>
<td width="516" valign="top" style="width:387.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; line-height:105%">
<span style="">
<span style="">P3-Moderate </span>
</span>
</p>
</td>
</tr>
<tr style="height:17.35pt">
<td width="150" valign="top" style="width:112.25pt; border:solid windowtext 1.0pt; border-top:none; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<p class="MsoNormal" align="right" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; text-align:right; line-height:105%">
<span style="">
<b>
<span style="color:red">Short Description: </span>
</b>
</span>
</p>
</td>
<td width="516" valign="top" style="width:387.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<span style=""></span>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; line-height:105%">
<span style="">
<span style=""> </span>
</span>
</p>
</td>
</tr>
<tr style="height:17.35pt">
<td width="150" valign="top" style="width:112.25pt; border:solid windowtext 1.0pt; border-top:none; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<p class="MsoNormal" align="right" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; text-align:right; line-height:105%">
<span style="">
<b>
<span style="">Description: </span>
</b>
</span>
</p>
</td>
<td width="516" valign="top" style="width:387.0pt; border-top:none; border-left:none; border-bottom:solid windowtext 1.0pt; border-right:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt; height:17.35pt">
<span style=""></span>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif; line-height:105%">
<span style="">
<span style=""> </span>
</span>
</p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style=""></span>
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style=""> </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<u>
<span lang="EN-US" style="font-size:13.0pt">NOTE: Request user to contact CMS Operations Team at +91-7337894153/8939984385 for Critical Incidents - P1 with business impact. </span>
</u>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<b>
<span lang="EN-US" style=""> </span>
</b>
</span>
</p>
<p class="MsoNormal" style="margin:0cm; font-size:11pt; font-family:Calibri,sans-serif">
<span style="">
<span lang="EN-US" style="font-size:10.5pt; color:black">Thanks,
<br>CMS Support
</span>
</span>
<span style="">
<b>
<span lang="EN-US" style=""> </span>
</b>
</span>
</p>
<br>
</div>
</body>
</html>
public class FetchHTMLBody {
private static final Logger logger = Logger.getLogger(FetchHTMLBody.class.getName());
public static void main(String[] args) {
String htmlContent="<html Content mentioned above>"
Document doc = Jsoup.parse(htmlContent);
List<String> keys = new ArrayList<>();
List<Map<String, String>> dataPairs = new ArrayList<>();
Elements trElements = doc.getElementsByTag("tr");
//logger.info("trElements::" + trElements);
for (int i = 0; i < trElements.size(); i++) {
Element element = trElements.get(i);
Elements pElements = element.getElementsByTag("td");
//logger.info("pElements::" + pElements);
Map<String, String> map = new HashMap<>();
for (int i1 = 0; i1 < pElements.size(); i1++) {
Element p = pElements.get(i1);
if (i == 0) {
keys.add(p.text());
} else {
map.put(keys.get(i1), p.text());
}
}
dataPairs.add(map);
logger.info("trElements::" + dataPairs);
}
}
}
[{}, {Ticket Type: =Company: , Incident =Grupo Bimbo DSDE }, {Ticket Type: =Configuration Item: , Incident = }, {Ticket Type: =Priority: , Incident =P3-Moderate }, {Ticket Type: =Short Description: , Incident = }, {Ticket Type: =Description: , Incident = }]
预期输出如下所示:
Ticket Type=Incident,
Company=Grupo Bimbo DSDE,
Configuration Item=null,
Priority=P3-Moderate,
Short Description=null,
Description=null
有人能帮我解决这个问题吗?
在调试了Java代码之后,我找到了我的用例的解决方案。
解决方案代码:
Document doc = Jsoup.parse(htmlContent);
List<String> keys = new ArrayList<>();
List<Map<String, String>> dataPairs = new ArrayList<>();
Map<String, String> map = new HashMap<>();
Elements trElements = doc.getElementsByTag("tr");
//logger.info("trElements::" + trElements);
for (int i = 0; i < trElements.size(); i++) {
Element element = trElements.get(i);
Elements pElements = element.getElementsByTag("td");
//logger.info("pElements::" + pElements);
for (int i1 = 0; i1 < pElements.size(); i1++) {
Element p = pElements.get(i1);
if (i1 == 0)
keys.add(p.text());
else
map.put(keys.get(i), p.text());
}
}
logger.info("Map::" + map.entrySet());
产出:
[Configuration Item: = , Ticket Type: =Incident , Description: = , Priority: =P3-Moderate , Short Description: = , Company: =Grupo Bimbo DSDE ]
我有这个html 并且,我试图得到每个标签的href。 例如,
来自检查器( 已展开) 从jsoup.connect( 未展开) 共1个答案 匿名用户 相关问题 Javamail不阅读多部分/相关的电子邮件 阅读从GMail发送的邮件 阅读Kafka时Spark丢失99.9%的消息 从 ajax 调用中的服务器接收 HTML 并使用该 HTML 加载页面 阅读主题并使用reactor Kafka将消息批量写入RESTendpoint 在HtmlUnit驱动程序
主要内容:Jsoup 读取URL获取HTML 语法,Jsoup 读取URL获取HTML 说明,Jsoup 读取URL获取HTML 示例以下示例将展示使用 url 从网络获取 HTML,然后查找其数据。 Jsoup 读取URL获取HTML 语法 document : 文档对象代表 HTML DOM。 Jsoup : 连接 url 并获取 HTML 字符串的主类。 url : 要加载的 html 页面的 url。 Jsoup 读取URL获取HTML 说明 connect(url) 方法连接到 url
问题内容: 是否可以读取已加载到中的网页的原始HTML内容? 如果不是,是否还有另一种方法可以从iPhone SDK中的网页中提取原始HTML内容(例如等效于.NET的内容)? 问题答案: 第二个问题实际上更容易回答。查看NSString的方法-它使您可以传入URL作为NSURL的实例(可以很容易地从NSString实例化),并在该URL返回包含页面完整内容的字符串。例如: 运行此代码后,将包含w
我一直在研究用于数据提取的Jsoup示例,并提取了此链接的一个示例 J汤
主要内容:Jsoup 读取文件获取HTML 语法,Jsoup 读取文件获取HTML 说明,Jsoup 读取文件获取HTML 示例以下示例将展示使用文件从磁盘获取 HTML,然后查找其数据。 Jsoup 读取文件获取HTML 语法 document : 文档对象代表 HTML DOM。 Jsoup : 连接 url 并获取 HTML 字符串的主类。 url : 要加载的 html 页面的 url。 Jsoup 读取文件获取HTML 说明 connect(url) 方法建立到 url 的连接,get