0

CSS Selector are not only used for html page authorization but more and more for web scraping too.

My question is, is it possible to use pure CSS Selector to identify the first (or second) hit out of a set of hits. For e.g., the google search,

https://www.google.ca/search?q=CSS+Selector+of+the+first+hit

Will yield a number of hits, and each hit can be identified by (is under)

div.rc

Say div.rc gives me 10 hits, i.e., 10 google search results,
is it possible to use pure CSS Selector to pin-point the first hit, hit#1?

I think none of :nth-child(n), :first-child, & ::first-line is what I'm asking for, and the .first() in "https://stackoverflow.com/questions/30628520/" is not a pure CSS Selector, right? Thx.

If all else fails and there is no such CSS Selector, then how about jquery Selector? Thx.

xpt
  • 20,363
  • 37
  • 127
  • 216
  • *"CSS Selector are not only used for html page authorization"* - That's an odd thing to suggest as a use of CSS selectors. Anyway, I'd have thought `:nth-child()` could work if applied to elements that are siblings. If you're using jQuery, why not just use the `.eq()` method? – nnnnnn Aug 01 '17 at 03:44
  • 1
    @nnnnnn His problem is that the `div.rc` elements are not siblings, they're each wrapped in several other divs. – Barmar Aug 01 '17 at 03:50
  • `$("div.rc:first")` should work in jQuery. – Barmar Aug 01 '17 at 03:51
  • @Barmar - So `"someparentelement:nth-child(1) div.rc"`? (For pure CSS, I mean. With jQuery extensions there's no problem.) – nnnnnn Aug 01 '17 at 03:51
  • Possible duplicate of https://stackoverflow.com/q/12379468/1817690 – Rohan Kumar Aug 01 '17 at 04:07

1 Answers1

0

The div.rc elements are all wrapped in div.g elements, which are siblings. So you can use:

div.g:first-child > div.rc

In jQuery you can use the :first extension.

$("#div.rc:first")
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • To be clear, there is no CSS equivalent to jQuery's first. You have to make assumptions about the structure in order to construct an appropriate selector - which isn't always possible (depending on the sort of pages you're scraping, anyway). – BoltClock Aug 01 '17 at 04:20