0

I'm trying to develope javascript code that matches every Span Tag with with the classname 'a-size-base a-color-price s-price a-text-bold' and returns in an Array the innerhtml of it. So basically I need the price of every spam in an array list.

Can somebody show me how to do this because I have no idea how to put the match function with the classname together and take the innerHTML of it.

<span class="a-size-base a-color-price s-price a-text-bold">EUR 16,90</span>
<span class="a-size-base a-color-price s-price a-text-bold">EUR 20,09</span> 
<span class="a-size-base a-color-price s-price a-text-bold">EUR 19,90</span> 
<span class="a-size-base a-color-price s-price a-text-bold">EUR 12,90</span> 

5
  • Please post your attempted code in your question. Commented Jan 5, 2018 at 19:37
  • 1
    var prices = [...document.querySelectorAll('span.a-size-base')].map(el => el.textContent.split(' ')[1]); Commented Jan 5, 2018 at 19:40
  • Should I write anything infront of ...document.querySelectorAll? What does this do? Is it like document.getElementbyClassName? Commented Jan 5, 2018 at 19:47
  • 1
    @JPqnp It works like a CSS selector Commented Jan 5, 2018 at 19:52
  • @Jared Smith Thanks! that code helped a lot! But is there a way to select more than two characters because at the end of your code is [1] which let me print only the first alphabetical Character but I need the first, second, third and fourth character. Commented Jan 5, 2018 at 20:50

2 Answers 2

1

If your input is like the sample provided, this should work:

// Since .querySelectorAll returns a NodeList and we want an
// Array we'll use the spread operator to make it one.
let prices = [...document.querySelectorAll('span.size-a-base')]
  .map(el => {
    // sample input was EUR 19,90 so we'll split on the space
    // and grab the price.
    let [currency, price] = el.textContent.trim().split(' ');
    return price;
  });
Sign up to request clarification or add additional context in comments.

Comments

0

Here you go a regex to get the price of your span/class=value.
What you're looking for is in capture group 2.

/<span(?=(?:[^>"']|"[^"]*"|'[^']*')*?\sclass\s*=\s*(?:(['"])\s*a-size-base\s*a-color-price\s*s-price\s*a-text-bold\s*\1))\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]*?)+>([\S\s]*?)<\/span\s*>/

(Warning, parsing html with regex can be hazardous)

https://regex101.com/r/myziY9/1

Scraper Series
Formatted:

 < span 
 (?=                                                           # Assertion (a pseudo atomic group)
      (?: [^>"'] | " [^"]* " | ' [^']* ' )*?
      \s 
      class \s* = \s*                                               # class attribute
      (?:
           ( ['"] )                                                      # (1), # quote begin
           \s* 
           a-size-base \s* a-color-price \s* s-price \s* a-text-bold     # value to find
           \s* 
           \1                                                            # quote end
      )
 )
 \s+ 
 (?: " [\S\s]*? " | ' [\S\s]*? ' | [^>]*? )+
 >
 ( [\S\s]*? )                                                  # (2), content
 </span \s* >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.