i am trying to scrape a web page, here is the JS code:
const axios = require('axios');
const cheerio = require('cheerio');
const r = 704290;
const link = 'https://www.discogs.com/sell/release/';
const link_completo = link + r.toString();
const headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0',
'Referer': 'http://www.discogs.com'
};
console.log(link_completo);
axios.get(link_completo, { headers })
.then((response) => {
const $ = cheerio.load(response.data);
const artist = $('h1').text();
console.log('Artist:', artist.trim());
});
here is the HTML:
<h1 class="title_1q3xW">
<span class="link_15cpV">
<a href="/artist/20991-The-Rolling-Stones" hreflang="en" class="link_1ctor link_15cpV">The Rolling Stones</a>
</span> – <!-- -->Black And Blue
</h1>
here is the output:
Artist: The Rolling Stones
–
Black And Blue
when i change the line
const artist = $('h1').text();
to get accurately the Artist name (The Rolling Stones)
const artist = $('.title_1q3xW').text()
i get no response
same with:
const artist = $('.link_15cpV').text()
This is my first question in Stack Overflow, be patience please if the question can be silly. Thank you for patience
const artist = $('.link_15cpV').text();
console.log(artist);
<h1 class="title_1q3xW"><span class="link_15cpV">
<a href="/artist/20991-The-Rolling-Stones" hreflang="en" class="link_1ctor link_15cpV">The Rolling Stones</a></span> –
<!-- -->Black And Blue</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
If all you want is The Rolling Stones that is located in the a tag.
Something like:
$('h1 a').text();
might work. Here's how it works in JavaScript in the browser.
document.getElementById('artist-name').innerText = document.querySelector('h1 a').text;
<h1 class="title_1q3xW"><span class="link_15cpV">
<a href="/artist/20991-The-Rolling-Stones" hreflang="en" class="link_1ctor link_15cpV">The Rolling Stones</a></span> –
<!-- -->Black And Blue</h1>
<h2>Inserting Artist Name Below<h2>
<h2 id="artist-name"></h2>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With