CSS Selector
DeprecatedView all Actors
This Actor is unavailable because the developer has decided to deprecate it. Would you like to try a similar Actor instead?
See alternative ActorsCSS Selector
jupri/css-scraper
Scrape a Webpage using CSS Selector
.actor/actor.json
1{
2 "actorSpecification": 1,
3 "name": "CSS scraper",
4 "title": "CSS scraper",
5 "description": "Scrape webpage using CSS selector.",
6 "version": "0.0.1",
7 "storages": {
8 "dataset": {
9 "actorSpecification": 1,
10 "title": "CSS scraper",
11 "views": {
12 "overview": {
13 "title": "JSON",
14 "transformation": {
15 "fields": [
16 "JSON"
17 ]
18 },
19 "display": {
20 "component": "table",
21 "columns": [
22 {
23 "label": "JSON",
24 "format": "text",
25 "field": "JSON"
26 }
27 ]
28 }
29 }
30 }
31 }
32 }
33}
src/main.js
1import { Actor } from 'apify'
2import { JSDOM } from 'jsdom'
3
4await Actor.init()
5
6console.log('Loading input')
7
8// Structure of input is defined in INPUT_SCHEMA.json.
9const input = await Actor.getInput()
10const {url, css} = input
11
12console.log('URL:', url)
13console.log('CSS:', css)
14
15// load dom, root = parent of window
16var root = await JSDOM.fromURL( url )
17
18// select from body
19var items = root.window.document.body.querySelectorAll( css )
20
21// get attributes
22var res = Array.prototype.map.call( items, item => Object.fromEntries( Object.values( item.attributes ).map(a => [a.name, a.value] ) ) )
23console.log( res.length, 'results' )
24
25// Structure of output is defined in .actor/actor.json
26await Actor.pushData( res.map(e => ({JSON: JSON.stringify(e)}) ) )
27
28await Actor.exit()
.dockerignore
1# configurations
2.idea
3
4# crawlee and apify storage folders
5apify_storage
6crawlee_storage
7storage
8
9# installed files
10node_modules
Dockerfile
1# Specify the base Docker image. You can read more about
2# the available images at https://sdk.apify.com/docs/guides/docker-images
3# You can also use any other image from Docker Hub.
4FROM apify/actor-node:16
5
6# Copy just package.json and package-lock.json
7# to speed up the build using Docker layer cache.
8COPY package*.json ./
9
10# Install NPM packages, skip optional and development dependencies to
11# keep the image small. Avoid logging too much and print the dependency
12# tree for debugging
13RUN npm --quiet set progress=false \
14 && npm install --omit=dev --omit=optional \
15 && echo "Installed NPM packages:" \
16 && (npm list --omit=dev --all || true) \
17 && echo "Node.js version:" \
18 && node --version \
19 && echo "NPM version:" \
20 && npm --version
21
22# Next, copy the remaining files and directories with the source code.
23# Since we do this after NPM install, quick build will be really fast
24# for most source file changes.
25COPY . ./
26
27
28# Run the image.
29CMD npm start --silent
INPUT_SCHEMA.json
1{
2 "title": "Add two integers",
3 "type": "object",
4 "schemaVersion": 1,
5 "properties": {
6 "url": {
7 "title": "Url",
8 "type": "string",
9 "description": "",
10 "editor": "textfield",
11 "prefill":"https://www.amazon.com/s?k=cat"
12 },
13 "css": {
14 "title": "CSS",
15 "type": "string",
16 "description": "CSS Selector",
17 "editor": "textfield",
18 "prefill":"div[data-asin][data-index]"
19 }
20 },
21 "required": ["url","css"]
22}
package.json
1{
2 "name": "getting-started-node",
3 "version": "0.0.1",
4 "type": "module",
5 "description": "This is an example of an Apify actor.",
6 "dependencies": {
7 "apify": "^3.0.0",
8 "jsdom": "^20.0.0"
9 },
10 "devDependencies": {},
11 "scripts": {
12 "start": "node src/main.js",
13 "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
14 },
15 "author": "It's not you it's me",
16 "license": "ISC"
17}
Developer
Maintained by Community
Categories