【发布时间】:2015-11-02 05:31:23
【问题描述】:
通过 URL 链接。例如:http://www.engineersireland.ie/home.aspx
我可以使用 java.net.URL 或 Jsoup 中内置的 java 来读取它们。
然后,我需要提取标签后标签之间的所有文本内容。
标签内会有标签。我只需要中间的文字。
例如:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head id="head"><title>
Engineers Ireland - Home
</title><meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-script-type" content="text/javascript" />
<link href="/favicon.ico" type="image/x-icon" rel="shortcut icon"/>
<link href="/favicon.ico" type="image/x-icon" rel="icon"/>
<body>
<div class="module-content">
<p id="1">Members can login for access to exclusive content, event booking, shop discounts and more...</p>
<fieldset>
<legend>Your Login Details</legend>
<div class="formline">
<label for="1" id="1">Your Membership Number</label>
<input name="1" type="text" id="1" title="Your Membership Number" class="login-username clearlabel" />
<span id="1e" class="ErrorLabel" style="display:none;">Enter your membership number</span>
</div>
<div class="formline">
<label for="1" id="adasdasd">Password</label>
<input name="asdasd" type="password" id="dfbsdf" title="Password" class="login-password clearlabel" />
<span id="drthd" class="ErrorLabel" style="display:none;">Enter your password</span>
</div>
<div class="formline">
<input name="aseresrr" type="checkbox" id="bstg" class="login-remember" />
<label for="ryjmf" id="asrats" class="remember">Remember Me</label>
<div class="button grey">
<input type="submit" name="fgn" value="LOGIN" onclick="sdf;, false, false))" id="sdfsdf" />
</div>
</div>
</fieldset>
<ul class="arrow">
<li><a href="/site/reset-password.aspx">Forgot your password?</a></li>
<li><a href="/membership/apply.aspx">Haven't registered yet?</a></li>
</ul>
</div>
</body>
</html>
从这段 html 代码中,我只需要:
Your Membership Number
Enter your membership number
Password
Enter your password
Remember Me
还有一点,
Keep in mind, the tag names and the number of tag are always random depend on the web page iteself.
有什么帮助吗?通过使用 Jsoup 还是 java?谢谢
【问题讨论】:
-
你有没有尝试过?你看过 JSoup 文档吗?对于你想要什么文本和你不想要什么文本有什么规则?它看起来像
<fieldset/>中所有<label/>和<span/>元素的文本内容,仅此而已?这些真的是要求吗?例如,为什么会排除<p/>标记中的文本或底部的链接? -
是的。我查了 Jsoup 的文档。现在正在研究它。对于上面的示例,它只是一个随机的。我需要 之后的任何标签之间的所有文本。标签可能包含标签,但我需要所有这些标签层中间的文本。谢谢
-
如果您仍在研究,只需向下滚动即可。我发布了一个工作示例。
标签: java html dom html-parsing jsoup