Home > Mobile >  DOM HTML Extract with javascript
DOM HTML Extract with javascript

Time:06-19

I'm trying to extract information from an html page, specifically I'm trying to get the "src" url below in bold (**)

<div  data-v-1a4e2f4c="">
<div index="0"  data-v-744a5232="" data-v-1a4e2f4c="">
    <div  data-v-744a5232=""><a
            href="/libro-el-peligro-de-estar-cuerda/9788432240645/12789134" title="EL PELIGRO DE ESTAR CUERDA"
             data-v-744a5232=""><img title="el peligro de estar cuerda-9788432240645"
                alt="el peligro de estar cuerda-9788432240645"
                **src="https://imagessl5.casadellibro.com/a/l/t1/45/9788432240645.jpg"**
                data-src="https://imagessl5.casadellibro.com/a/l/t1/45/9788432240645.jpg" width="" height=""
                 style="max-height:undefinedpx;max-width:undefinedpx;"
                data-v-744a5232=""></a></div>

The code I'm using does not seem to do the trick, although it works when extracting the title:

  let resultImg = xmlDoc.evaluate('./div/div/a/img[@src]', node, null, XPathResult.FIRST_ORDERED_NODE_TYPE);
    let bookImgSrc = resultImg.singleNodeValue.src;
    imgCard.src = bookImgSrc.replace('mtiny', 'large');
    divCard.appendChild(imgCard);
    console.log(imgCard);

Could someone point out what is wrong in the code and how to get the src url?

I suspect the xPathResult may be wrong.

CodePudding user response:

you can select the element then use this following code :

var myElementSrc = mySelectedElement.src ;

this is simple.

CodePudding user response:

I don't get why you add bold sign (**) in HTML tag, it would make your tag useless. if you write the as:

         <img title="el peligro de estar cuerda-9788432240645"
              alt="el peligro de estar cuerda-9788432240645"
              src="https://imagessl5.casadellibro.com/a/l/t1/45/9788432240645.jpg"
              data-src="https://imagessl5.casadellibro.com/a/l/t1/45/9788432240645.jpg"
              width=""
              height=""
              
              style="max-height: undefinedpx; max-width: undefinedpx"
              data-v-744a5232=""
          />

your code might just work fine.

  • Related