Browse HTML page and search for links

Asked

Viewed 864 times

0

I do not know if the title was clarified but I would like to know how to go through the body of an html page and identify links by exceptions... Let’s just say I want links that have https://site.com/imagens/ in your url.

1 answer

4


See if this example using jQuery suits you. Get all links in the address (href) to string "https://site.com/imagens/". With the object of the links you can do whatever you need. In the example below, I put to display in a div.

function ObterLinks()
{
  var links = $('a[href*="https://site.com/imagens/"]'); //obtém todos os links que contenham o endereço.
  var divResultado = $('#divResultado');
  for(var i = 0; i < links.length; i++)
  {
    divResultado.append('<p>' + links[i].href + '</p>');
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="links">
  <a href="https://site.com/imagens/1.jpg"> Link 1 </a>
  <br>
  <a href="https://site.com/imagens/2.jpg"> Link 2 </a>
  <br>
  <a href="https://site.com/imagens/3.jpg"> Link 3 </a>
  <br>
  <a href="https://site.com/imagens/4.jpg"> Link 4 </a>
</div>
<br>
<button onclick="ObterLinks()">Obter Links </button>

<div id="divResultado">

</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.