Search code examples
javascriptjqueryhtmlweb-crawlerapify

Diffrent content on the page Apify Webcrawling


I have a question about crawling websites with Apify. I want to get the following information from the webpage : " Location, Area, Website & Email. With my code now I get the right data for a few pages, the problem is the order of the information isn't the same on every page. for example the Phone number is the first one on a page and third one on a other page. Is there a way to look for the text in the "dt" part of the code, so i get the right data in the right column? or is there an other option to fix this problem?

this is my code now: const $ = jQuery

if ( request.userData.label === 'Deals'){
const location = $('.info-list dd:nth-of-type(1)').text();
const area = $('.info-list dd:nth-of-type(2)').text();
const website = $('.info-list dd:nth-of-type(4)').text();
const email = $('.info-list dd:nth-of-type(5)').text();

this is the Html code:

<dl class="info-list">
<dt>Location</dt><dd>.....</dd><dt>Area</dt><dd>Cape Town CBD</dd><dt>Phone</dt><dd class="number">......</dd><dt>Website</dt><dd><a target="_blank" href=.......>http://15onorangebyautograph.com</a></dd><dt>Email</dt><dd><a href="mailto:[email protected]">[email protected]</a></dd>
</dl>

Thanks for helping


Solution

  • Yes, with JQuery you can query on text too with [:contains()](https://api.jquery.com/contains-selector/#contains1)

    So you can get the dt that contains the keyword and then the next one is dd that has the data you need:

    const location = $('.info-list dt:contains("Location")').next().text();