3

I am scraping bing search results using node and cheerio. I need to grab all the href values from two lists that have different IDs.

  1. How can I grab all the tags from both these lists in one statement? I tried but it didn't work.
  2. From the first list, I do not want the li tags with the class "b_pag". How can I write a selector for it? Like a Not condition or something.

$("a", ["#b_content", "#b_context"]).each((index, element) => { const href = $(element).attr("href"); links.push(href); });

Refer to the attached screenshot for the html.html

Update2 : I was wanting to ignore the whole <li class="b_pag"> tag, but the solutions I found here and elsewhere ignored just that tag. Any other <li> tag under it, which has any other or no class, does not get ignored.

I found a way around it. I could grab the <li> tags that have other class names. Check out the html here. I am thinking of using four different selectors for the first four classes. Like $(.b_algo) or $(.b_ans). But how can I grab the other two <li> tags that have multiple classes associated with it? I could not get a clear idea from the cheerio docs. Hope I am clear enough for you guys! Something like $(.b_ans b_mop) didn't work. Nor did $("li[class=b_ans b_mop").

Rohit
  • 1,385
  • 2
  • 15
  • 21

3 Answers3

3

Try this,

$("#b_content", "#b_context").each(function(i, elem) {
        array[i] = {
             a: $(this).find("a").attr("href")
         };
      });`

To select "li" except class "b_pag" use, li:not( .b_pag )

Dipesh Lohani
  • 306
  • 3
  • 11
  • $("#b_content", "b_context") will find b_content under b_context, which doesn't exist, so I get an empty result. the "li:not(.b_pag)" is what I was looking for. Could you tell me where exactly would you write it in the code? I can grab the
      { // something });` Then where should I insert the not condition?
    – Rohit Feb 27 '19 at 06:40
  • 1
    Here you go, $("#b_results").find("li:not(.b_pag)").each((i, el) => { // something }); – Dipesh Lohani Feb 27 '19 at 10:48
  • I found a way around that problem. I have another doubt though. how can I grab an element like this -
  • ? does this mean that this element is associated with 3 classes (a, b and c)?
  • – Rohit Mar 05 '19 at 05:59
  • 1
    Yes ! Similar to some bootstrap classes .. – Dipesh Lohani Mar 05 '19 at 10:01
  • if there is a
  • and another
  • and I do something like `$(".a")`, then it grabs both these elements. is there a way to pass multiple classes in one selector? so that I can grab only the second
  • using something like `$("a b") maybe.
  • – Rohit Mar 05 '19 at 11:16
  • 1
    The answer to this question has been answered before, check this out. [link](https://stackoverflow.com/questions/1041344/how-can-i-select-an-element-with-multiple-classes-in-jquery) – Dipesh Lohani Mar 06 '19 at 06:00
  • could you help me with [this](https://stackoverflow.com/questions/55001879/can-we-not-access-the-div-id-botstuff-present-in-the-dom-of-the-google-searc) ? @Dipesh – Rohit Mar 06 '19 at 09:01