This guide walks you through advanced CSS selector strategies to help you extract structured data from a wide variety of web layouts using the ZenRows API.
Target elements by their tag names, classes, or IDs:
Copy
Ask AI
css_extractor: JSON.stringify({ headings: "h1", // Select all <h1> elements products: ".product", // Select elements with the 'product' class mainContent: "#main-content", // Select the element with the 'main-content' ID productTitles: ".product h2.title", // Select <h2> elements with the 'title' class inside 'product' class topLevelNav: "nav > a" // Select direct child <a> elements of <nav>})
css_extractor: JSON.stringify({ imageUrls: "img @src", // Extract 'src' attribute from <img> tags linkUrls: "a @href", // Extract 'href' attribute from <a> tags premiumItems: "[data-premium='true']", // Select elements with a specific attribute value externalLinks: "[href^='https://'] @href", // Extract 'href' starting with 'https://' pdfDownloads: "[href$='.pdf'] @href" // Extract 'href' ending with '.pdf'})
Target elements based on their position in the document:
Copy
Ask AI
css_extractor: JSON.stringify({ firstProduct: ".product:first-child", // Selects the first element with the class 'product' lastProduct: ".product:last-child", // Selects the last element with the class 'product' thirdProduct: ".product:nth-child(3)", // Selects the third element with the class 'product' evenProducts: ".product:nth-child(even)", // Selects all even-numbered 'product' elements oddProducts: ".product:nth-child(odd)", // Selects all odd-numbered 'product' elements firstHeading: "h2:nth-of-type(1)", // Selects the first <h2> element of its type tableHeaders: "table th", // Selects all <th> elements inside any <table> secondColumnCells: "tr td:nth-child(2)" // Selects the second <td> element in each table row})
css_extractor: JSON.stringify({ productNames: ".product-item .product-title", // Selects the product title within each product item productPrices: ".product-item .price", // Selects the price element within each product item productRatings: ".product-item .rating @data-score", // Extracts the 'data-score' attribute from the rating element productImages: ".product-item img.product-image @src", // Extracts the 'src' attribute from the product image productUrls: ".product-item a.product-link @href", // Extracts the 'href' attribute from the product link productAvailability: ".product-item .availability-badge", // Selects the availability badge within each product item productDiscounts: ".product-item .discount-tag" // Selects the discount tag element within each product item})
css_extractor: JSON.stringify({ // Main navigation mainNavLinks: ".main-nav > li > a @href", mainNavText: ".main-nav > li > a", // Second level categories subNavLinks: ".main-nav > li > .dropdown > a @href", subNavText: ".main-nav > li > .dropdown > a", // Third level deepNavLinks: ".main-nav > li > .dropdown > .sub-dropdown > a @href"})