JSOUP を使用して、html 本文からリンクをフィルタリングしています。
私はこれらのセレクターを使用します:
Elements links = doc.select("a[href]"); // Select all links
links.select("a[href*=#]").remove(); // remove links containing #
ただし、ハッシュタグを含むリンクはまだあります。これはどのように可能ですか?
Elementsのremove()
メソッドは、Elements 自体から一致を削除するのではなく、関連する Document オブジェクトから削除します。
たとえば、次の場合:
<html>
<body>
<a href="#someid"></a>
<a href="http://www.google.pt"></a>
</body>
</html>
あなたがした後links.select("a[href*=#]").remove();
:
<html>
<head></head>
<body>
<a href="http://www.google.pt"></a>
</body>
</html>
ハッシュタグ以外のリンクをすべて選択したい場合は、次のようにします。
Elements links = doc.select("a[href~=[^#]*");