簡體   English   中英

如何使用 JSOUP 提取 css 樣式

[英]How to extract the css style with 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 以及特殊的 class 風格的風格? 提前致謝

內部 css 描述為樣式屬性,而不是像內聯 css 這樣的元素 因此,您必須使用 attr() 方法獲取內部 css 。 例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM