fetch-ecommerce-image
Go to Store
This Actor is unavailable because the developer has decided to deprecate it. Would you like to try a similar Actor instead?
See alternative Actorsfetch-ecommerce-image
red_vault/fetch-ecommerce-image
Get amazon and flipkart product image
.actor/Dockerfile
1# Specify the base Docker image. You can read more about
2# the available images at https://docs.apify.com/sdk/js/docs/guides/docker-images
3# You can also use any other image from Docker Hub.
4FROM apify/actor-node:18
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 && rm -r ~/.npm
22
23# Next, copy the remaining files and directories with the source code.
24# Since we do this after NPM install, quick build will be really fast
25# for most source file changes.
26COPY . ./
27
28
29# Run the image.
30CMD npm start --silent
.actor/actor.json
1{
2 "actorSpecification": 1,
3 "name": "fetch-amazon-image",
4 "title": "Project Cheerio Crawler Javascript",
5 "description": "Crawlee and Cheerio project in javascript.",
6 "version": "0.0",
7 "meta": {
8 "templateId": "js-crawlee-cheerio"
9 },
10 "input": "./input_schema.json",
11 "dockerfile": "./Dockerfile"
12}
.actor/input_schema.json
1{
2 "title": "CheerioCrawler Template",
3 "type": "object",
4 "schemaVersion": 1,
5 "properties": {
6 "pageurl": {
7 "title": "URL",
8 "type": "string",
9 "editor": "textfield",
10 "description": "Product URL",
11 "default": "https://crawlee.dev"
12 },
13 "asincode": {
14 "title": "asincode",
15 "type": "string",
16 "editor": "textfield",
17 "description": "Asin code for the product",
18 "default": "B0B4N77Y34"
19 },
20 "type": {
21 "title": "Type: asin or search",
22 "type": "string",
23 "editor": "textfield",
24 "description": "Must be one from asin or search",
25 "default": "asin"
26 },
27 "service": {
28 "title": "Type: amazon or flipkart",
29 "type": "string",
30 "editor": "textfield",
31 "description": "Must be one amazon or flipkart",
32 "default": "amazon"
33 },
34 "maxRequestsPerCrawl": {
35 "title": "Max Requests per Crawl",
36 "type": "integer",
37 "description": "Maximum number of requests that can be made by this crawler.",
38 "default": 100
39 }
40 }
41}
src/main.js
1// Apify SDK - toolkit for building Apify Actors (Read more at https://docs.apify.com/sdk/js/)
2import { Actor } from 'apify';
3// Crawlee - web scraping and browser automation library (Read more at https://crawlee.dev)
4import { CheerioCrawler, Dataset } from 'crawlee';
5import { PlaywrightCrawler } from 'crawlee';
6import { ProxyConfiguration } from 'apify';
7// this is ESM project, and as such, it requires you to specify extensions in your relative imports
8// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
9// import { router } from './routes.js';
10
11// The init() call configures the Actor for its environment. It's recommended to start every Actor with an init()
12await Actor.init();
13
14// Structure of input is defined in input_schema.json
15const {
16 pageurl = 'https://www.amazon.in/gp/aod/ajax?asin=B0D945V84N&ref=auto_load_aod&pc=dp',
17 asincode = 'B0B4N77Y34',
18 type = 'asin',
19 service = 'amazon',
20 maxRequestsPerCrawl = 2,
21} = await Actor.getInput() ?? {};
22
23//const proxyConfiguration = await Actor.createProxyConfiguration();
24const proxyConfiguration = new ProxyConfiguration({
25 groups: ['RESIDENTIAL'],
26 countryCode: 'US', // Optionally, you can specify the proxy country code.
27 // This is useful for sites like Amazon, which display different content based on the user's location.
28});
29
30if(type === 'search' && service === 'amazon'){
31 const crawler = new CheerioCrawler({
32 maxRequestRetries: 5,
33 proxyConfiguration,
34 maxRequestsPerCrawl,
35 async requestHandler({ request, $, log }) {
36 log.info('enqueueing new URLs');
37
38 const image = $('#landingImage').attr('src');
39 log.info('Log', { url: request.loadedUrl, image });
40
41 await Dataset.pushData({ image })
42 },
43 });
44
45 await crawler.run([pageurl]);
46}
47
48if(type === 'asin' && service === 'amazon'){
49 const crawler = new CheerioCrawler({
50 maxRequestRetries: 5,
51 proxyConfiguration,
52 maxRequestsPerCrawl,
53 handlePageFunction: ({ proxyInfo }) => {
54 const usedProxyUrl = proxyInfo.url; // Getting the proxy URL
55 log.info(usedProxyUrl)
56 },
57 async requestHandler({ request, $, log }) {
58 log.info('enqueueing new URLs');
59
60 const image = $('#aod-asin-image-id').attr('src');
61 log.info('Log', { url: request.loadedUrl, image });
62
63 await Dataset.pushData({ image })
64 },
65 });
66
67 await crawler.run([`https://www.amazon.in/gp/aod/ajax?asin=${asincode}&ref=auto_load_aod&pc=dp`]);
68}
69
70if(type === 'asin' && service === 'amazon_v2'){
71 const crawler = new PlaywrightCrawler({
72 maxRequestRetries: 5,
73 proxyConfiguration,
74 maxRequestsPerCrawl,
75 async requestHandler({ request, $, log }) {
76 log.info('enqueueing new URLs');
77
78 const image = $('#aod-asin-image-id').attr('src');
79 log.info('Log', { url: request.loadedUrl, image });
80
81 await Dataset.pushData({ image })
82 },
83 });
84
85 await crawler.run([`https://www.amazon.in/gp/aod/ajax?asin=${asincode}&ref=auto_load_aod&pc=dp`]);
86}
87
88if(type === 'search' && service === 'flipkart'){
89 const crawler = new CheerioCrawler({
90 maxRequestRetries: 5,
91 proxyConfiguration,
92 maxRequestsPerCrawl,
93 async requestHandler({ request, $, log }) {
94 log.info('enqueueing new URLs');
95
96 const ogImage = $('meta[property="og:image"]').attr('content');
97 const addToCartButton = $('button:contains("Add to cart")');
98 const buyNowButton = $('button:contains("Buy Now")');
99
100 if (ogImage) {
101 log.info('Log', { url: request.loadedUrl });
102
103 await Dataset.pushData({ image: ogImage });
104 } else if(addToCartButton.length == 0 && buyNowButton.length == 0) {
105 // get the first image from the search result
106
107 const images = $('div._1YokD2._2GoDe3 > div:nth-child(2) > div:nth-child(2) > div > div:nth-child(1) > div > a._2rpwqI > div:nth-child(1) > div > div > img');
108 const image = images[0];
109
110 const category = $("#container > div > div._36fx1h._6t1WkM._3HqJxg > div._1YokD2._2GoDe3 > div:nth-child(2) > div:nth-child(2) > div > div:nth-child(1) > div > a > div:nth-child(1) > div > div > div > img")
111 const categoryImage = category[0];
112
113 if(image){
114 log.info('Log', { url: request.loadedUrl });
115
116 await Dataset.pushData({ image: image.attribs.src });
117 }else if(categoryImage){
118 log.info('Log', { url: request.loadedUrl });
119
120 await Dataset.pushData({ image: categoryImage.attribs.src });
121 }
122 } else if(addToCartButton.length > 0 && buyNowButton.length > 0){
123 // get the landing page image
124
125 const image = $('img[loading="eager"]')[0];
126 if(image){
127 log.info('Log', { url: request.loadedUrl });
128
129 await Dataset.pushData({ image: image.attribs.src });
130 }
131 } else {
132 // If no Open Graph image is found, use a fallback (e.g., favicon)
133
134 const favicon = $('link[rel="icon"]').attr('href');
135 if (favicon) {
136 log.info('Log', { url: request.loadedUrl });
137
138 await Dataset.pushData({ image: favicon });
139 }
140 }
141 },
142 });
143
144 await crawler.run([pageurl]);
145}
146
147// Gracefully exit the Actor process. It's recommended to quit all Actors with an exit()
148await 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
11
12# git folder
13.git
.editorconfig
1root = true
2
3[*]
4indent_style = space
5indent_size = 4
6charset = utf-8
7trim_trailing_whitespace = true
8insert_final_newline = true
9end_of_line = lf
.eslintrc
1{
2 "extends": "@apify",
3 "root": true
4}
.gitignore
1# This file tells Git which files shouldn't be added to source control
2
3.DS_Store
4.idea
5dist
6node_modules
7apify_storage
8storage
oldmain.js
1// Apify SDK - toolkit for building Apify Actors (Read more at https://docs.apify.com/sdk/js/)
2import { Actor } from 'apify';
3// Crawlee - web scraping and browser automation library (Read more at https://crawlee.dev)
4import { CheerioCrawler, Dataset } from 'crawlee';
5// this is ESM project, and as such, it requires you to specify extensions in your relative imports
6// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
7// import { router } from './routes.js';
8
9// The init() call configures the Actor for its environment. It's recommended to start every Actor with an init()
10await Actor.init();
11
12// Structure of input is defined in input_schema.json
13const {
14 scrapurl = 'https://crawlee.dev',
15 type = 'asin',
16 maxRequestsPerCrawl = 100,
17} = await Actor.getInput() ?? {};
18
19const proxyConfiguration = await Actor.createProxyConfiguration();
20
21if(type === 'asin'){
22 const crawler = new CheerioCrawler({
23 maxRequestRetries: 5,
24 proxyConfiguration,
25 maxRequestsPerCrawl,
26 async requestHandler({ request, $, log }) {
27 log.info('enqueueing new URLs');
28
29 // Extract title from the page.
30 //const title = $('title').text();
31
32 const image = $('#landingImage').attr('src');
33 log.info('Log', { url: request.loadedUrl, image: image });
34
35 if(!image) return request.pushErrorMessage("Failed to fetch image");
36
37 // Save url and title to Dataset - a table-like storage.
38 await Dataset.pushData({ image })
39 },
40 });
41
42 await crawler.run([scrapurl]);
43}
44
45// Gracefully exit the Actor process. It's recommended to quit all Actors with an exit()
46await Actor.exit();
package.json
1{
2 "name": "crawlee-cheerio-javascript",
3 "version": "0.0.1",
4 "type": "module",
5 "description": "This is a boilerplate of an Apify actor.",
6 "engines": {
7 "node": ">=18.0.0"
8 },
9 "dependencies": {
10 "apify": "^3.1.10",
11 "crawlee": "^3.5.4"
12 },
13 "devDependencies": {
14 "@apify/eslint-config": "^0.4.0",
15 "eslint": "^8.50.0"
16 },
17 "scripts": {
18 "start": "node src/main.js",
19 "lint": "eslint ./src --ext .js,.jsx",
20 "lint:fix": "eslint ./src --ext .js,.jsx --fix",
21 "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
22 },
23 "author": "It's not you it's me",
24 "license": "ISC"
25}
Developer
Maintained by Community
Categories