java - 如何使用 JSOUP 提取 css 样式

标签 java css jsoup

我有来自系统剪贴板的 html,用于在 MS Excel 中复制数据,

我想提取带有样式的数据。这里的html内容包含了STYLE标签中的css,如下图

xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 15">
<link id=Main-File rel=Main-File
href="file:////Users/tikeshwar-1410/Library/Group%20Containers/UBF8T346G9.Office/TemporaryItems/msohtmlclip/clip.htm">
<link rel=File-List
href="file:////Users/tikeshwar-1410/Library/Group%20Containers/UBF8T346G9.Office/TemporaryItems/msohtmlclip/clip_filelist.xml">
<style>
<!--table
    {mso-displayed-decimal-separator:"\.";
    mso-displayed-thousand-separator:"\,";}
@page
    {margin:.75in .7in .75in .7in;
    mso-header-margin:.3in;
    mso-footer-margin:.3in;}
tr
    {mso-height-source:auto;}
col
    {mso-width-source:auto;}
br
    {mso-data-placement:same-cell;}
td
    {padding-top:1px;
    padding-right:1px;
    padding-left:1px;
    mso-ignore:padding;
    color:black;
    font-size:12.0pt;
    font-weight:400;
    font-style:normal;
    text-decoration:none;
    font-family:Calibri, sans-serif;
    mso-font-charset:0;
    mso-number-format:General;
    text-align:general;
    vertical-align:bottom; 
    border:none;
    mso-background-source:auto;
    mso-pattern:auto;
    mso-protection:locked visible;
    white-space:nowrap;
    mso-rotate:0;}
.xl63
    {font-weight:700;}
.xl64
    {background:yellow;
    mso-pattern:black none;}
.xl65
    {color:red;}
.xl66
    {text-decoration:underline;
    text-underline-style:single;}
-->
</style>
</head>

<body link="#0563C1" vlink="#954F72">

<table border=0 cellpadding=0 cellspacing=0 width=174 style='border-collapse:
 collapse;width:130pt'>
<!--StartFragment-->
 <col width=87 span=2 style='width:65pt'>
 <tr height=21 style='height:16.0pt'>
  <td height=21 class=xl63 width=87 style='height:16.0pt;width:65pt'>a</td>
  <td class=xl64 align=right width=87 style='width:65pt'>1</td>
 </tr>
 <tr height=21 style='height:16.0pt'>
  <td height=21 class=xl65 style='height:16.0pt'>b</td>
  <td class=xl66 align=right>2</td>
 </tr>
<!--EndFragment-->
</table>

</body>

</html>

我可以使用样式选择器获取 ay 元素的内联样式,但在这里我无法获取样式作为其内部标记。

有什么方法可以获得每个 TD、TR 以及特殊类样式的样式吗? 提前致谢

最佳答案

内部 css 描述为样式属性,而不是像内联 css 那样的元素。所以你必须使用 attr() 方法获取内部 css。例如:

Document htmlFile = Jsoup.parse(html);
Element firstTableElem = htmlFile.select("table").first();
String tableStyleValue = firstTableElem.attr("style"); //gives you internal css

关于java - 如何使用 JSOUP 提取 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58218933/

相关文章:

java - 从应用程序打开系统设置的特定部分

java - 如何在 Java 中监控外部文件

java - Jersey 1.17 速率限制

java - 如何重构这段IO代码?

javascript - 以 html/css 作为正文背景的网格

java - Jsoup 连接和重试

jquery - 制作 JS/CSS 菜单 - 悬停无法正常工作

css - 如何居中内容

java - 使用 jsoup java 从 &lt;!-- --> 注释中提取 HTML 到结束标记

java - 信任库中的 "Anchor for certification path not found"