¿Hay algún método por el cual pueda obtener la URL dentro de la etiqueta <img> desde un enlace?
Por ejemplo: cuando doy un enlace de un sitio web, como un sitio web o una página de Facebook, quiero obtener todas las URL dentro de todas las etiquetas img en ese sitio --> <*img src="url">
Primero envíe una solicitud a una página, obtenga el contenido y analícelo en html. Luego obtenga todas las etiquetas img usando el método getElementsByTagName .
Por ejemplo, si desea obtener todas las etiquetas img en https://jsonplaceholder.typicode.com , su código debería ser como el siguiente.
fetch('https://jsonplaceholder.typicode.com').then(function (response) { // The API call was successful! return response.text(); }).then(function (html) { // Convert the HTML string into a document object var parser = new DOMParser(); var doc = parser.parseFromString(html, 'text/html'); var imgTags = doc.getElementsByTagName("img"); console.log(imgTags); var imgTagArr = [...imgTags]; imgTagArr.forEach(img => console.log(img.src)); //iterate the all img tag srcs }).catch(function (err) { // There was an error console.warn('Something went wrong.', err); });Salida de ejemplo:
Sí, en Html como <img class="pqr" url ="xxxxxxx-yyy"/>
en el archivo Js:
const img =document.querySelector('.pqr') img.src // <-- this will give the src of the image