├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── MIGRATIONS.md ├── README.md ├── cheerio-scraper ├── .gitignore ├── .npmrc ├── Dockerfile ├── INPUT_SCHEMA.json ├── LICENSE ├── README.md ├── apify.json ├── main.js ├── package-lock.json ├── package.json └── src │ └── crawler_setup.js ├── docs ├── .eslintignore ├── .eslintrc.json ├── .gitignore ├── LICENSE ├── build │ ├── cheerio-scraper-tutorial.md │ ├── introduction-tutorial.md │ ├── puppeteer-scraper-tutorial.md │ └── web-scraper-tutorial.md ├── cheerio-scraper │ ├── cheerio-scraper.md │ ├── code │ │ ├── bonus.js │ │ ├── description.js │ │ ├── modified-date.js │ │ ├── pagination.js │ │ ├── run-count.js │ │ ├── title.js │ │ ├── wrapping-it-up-1.js │ │ └── wrapping-it-up-2.js │ └── partials │ │ ├── pagination.md │ │ └── tools.md ├── common │ └── partials │ │ ├── bonus.md │ │ ├── build-page-function.md │ │ ├── description.md │ │ ├── downloading-scraped-data.md │ │ ├── final-word.md │ │ ├── intro.md │ │ ├── modified-date.md │ │ ├── pagination-intro.md │ │ ├── run-count.md │ │ ├── title.md │ │ └── wrapping-it-up.md ├── img │ ├── actor-selection.webp │ ├── debugging-log.webp │ ├── debugging-run-mode.webp │ ├── description.webp │ ├── find-data.webp │ ├── inspect-data.webp │ ├── inspect-network.webp │ ├── last-run-date.webp │ ├── modified-date.webp │ ├── plugging-it-into-the-pagefunction.webp │ ├── scraping-practice.webp │ ├── task-name.webp │ ├── the-run-detail.webp │ ├── title.webp │ ├── using-devtools.webp │ └── waiting-for-the-button.webp ├── introduction │ ├── code │ │ └── test-run-2.js │ └── introduction.md ├── mustache.js ├── package-lock.json ├── package.json ├── puppeteer-scraper │ ├── code │ │ ├── bonus.js │ │ ├── description.js │ │ ├── jquery.js │ │ ├── modified-date.js │ │ ├── pagination.js │ │ ├── run-count.js │ │ ├── title.js │ │ ├── wrapping-it-up-1.js │ │ └── wrapping-it-up-2.js │ ├── partials │ │ ├── jquery.md │ │ ├── note-modified-date.md │ │ ├── note-title-1.md │ │ ├── note-title.md │ │ ├── note-wrapping-it-up-2.md │ │ ├── pagination.md │ │ └── tools.md │ └── puppeteer-scraper.md └── web-scraper │ ├── code │ ├── bonus.js │ ├── description.js │ ├── modified-date.js │ ├── pagination.js │ ├── run-count.js │ ├── title.js │ ├── wrapping-it-up-1.js │ └── wrapping-it-up-2.js │ ├── partials │ ├── debugging.md │ ├── pagination.md │ └── tools.md │ └── web-scraper.md ├── logos ├── logo_cheerio-scraper.png ├── logo_cheerio-scraper.svg ├── logo_web-scraper.png └── logo_web-scraper.svg ├── package-lock.json ├── package.json ├── puppeteer-scraper ├── .gitignore ├── Dockerfile ├── INPUT_SCHEMA.json ├── LICENSE ├── README.md ├── apify.json ├── main.js ├── package-lock.json ├── package.json └── src │ └── crawler_setup.js ├── scraper-tools ├── .gitignore ├── LICENSE ├── index.js ├── package.json ├── src │ ├── browser_tools.js │ ├── consts.js │ ├── context.js │ ├── run_actor.js │ └── tools.js └── test │ ├── .eslintrc.json │ ├── browser_tools.js │ └── tools.js └── web-scraper ├── .gitignore ├── Dockerfile ├── INPUT_SCHEMA.json ├── LICENSE ├── README.md ├── apify.json ├── main.js ├── package-lock.json ├── package.json ├── src ├── bundle.browser.js ├── consts.js ├── crawler_setup.js └── global_store.js └── test ├── .eslintrc.json └── bundle.browser.test.js /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@apify" 3 | } 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | */package-lock.json linguist-generated=true 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | apify_storage 3 | node_modules 4 | .nyc_output 5 | coverage 6 | */src/page_function.js 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /MIGRATIONS.md: -------------------------------------------------------------------------------- 1 | # Migration from 1.x to 2.0 2 | 3 | Main difference between v1 and v2 of the scrapers is the upgrade of SDK to v2, 4 | which requires node v15.10+. SDK v2 uses http2 to do the requests with `cheerio-scraper`, 5 | and the http2 support in older node versions were too buggy, so we decided to 6 | drop support for those. If you need to run on older node version, use SDK v1. 7 | 8 | Please refer to the [SDK 1.0 migration guide](https://sdk.apify.com/docs/guides/migration-to-v1) for more details about functional changes in the SDK. 9 | SDK v2 basically only changes the required node version and has no other breaking 10 | changes. 11 | 12 | - deprecated `useRequestQueue` option has been removed 13 | - request queue will be always used 14 | - deprecated `context.html` getter from the `cheerio-scraper` has been removed 15 | - use `context.body` instead 16 | - deprecated `prepareRequestFunction` input option 17 | - use `pre/postNavigationHooks` instead 18 | - removed `puppeteerPool`/`autoscaledPool` from the `crawlingContext` object 19 | - `puppeteerPool` was replaces by `browserPool` 20 | - `autoscaledPool` and `browserPool` and available on the `crawler` property of `crawlingContext` object 21 | - custom "Key-value store name" option in Advanced configuration is now fixed, previously the default store was always used 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apify Scrapers 2 | This repository houses all of Apify generic actors that are used for simplified 3 | scraping using a pre-defined, schema validated UI input instead of the typical 4 | JSON input used in other actors. 5 | 6 | ## Web Scraper 7 | **Web Scraper** ([apify/web-scraper](https://apify.com/apify/web-scraper)) is a ready-made solution for scraping the web using the Chrome browser. It takes away all the work necessary to set up a browser for crawling, controls the browser automatically and produces machine readable results in several common formats. 8 | 9 | Underneath, it uses the Puppeteer library to control the browser, but you don't need to worry about that. Using a simple web UI and a little of basic JavaScript, you can tweak it to serve almost any scraping need. 10 | 11 | ## Puppeteer Scraper 12 | **Puppeteer Scraper** ([apify/puppeteer-scraper](https://apify.com/apify/puppeteer-scraper)) is the most powerful scraper tool in our arsenal (aside from developing your own actors). It uses the Puppeteer library to programmatically control a headless Chrome browser and it can make it do almost anything. If using the Web Scraper does not cut it, Puppeteer Scraper is what you need. 13 | 14 | Puppeteer is a Node.js library, so knowledge of Node.js and its paradigms is expected when working with the Puppeteer Scraper. 15 | 16 | If you need either a faster, or a simpler tool, see the Cheerio Scraper for speed, or Web Scraper for simplicity. 17 | 18 | ## Cheerio Scraper 19 | **Cheerio Scraper** ([apify/cheerio-scraper](https://apify.com/apify/cheerio-scraper)) is a ready-made solution for crawling the web using plain HTTP requests to retrieve HTML pages and then parsing and inspecting the HTML using the Cheerio library. It's blazing fast. 20 | 21 | Cheerio is a server-side version of the popular jQuery library, that does not run in the browser, but instead constructs a DOM out of a HTML string and then provides the user with API to work with that DOM. 22 | 23 | Cheerio Scraper is ideal for scraping websites that do not rely on client-side JavaScript to serve their content. It can be as much as 20 times faster than using a full browser solution such as Puppeteer. 24 | 25 | ## Scraper Tools 26 | A library that houses logic common to all the scrapers. 27 | -------------------------------------------------------------------------------- /cheerio-scraper/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | apify_storage 3 | node_modules 4 | .nyc_output 5 | coverage 6 | src/page_function.js 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /cheerio-scraper/.npmrc: -------------------------------------------------------------------------------- 1 | optional=false 2 | -------------------------------------------------------------------------------- /cheerio-scraper/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM apify/actor-node:16 2 | 3 | COPY package.json package-lock.json ./ 4 | 5 | # Install default dependencies, print versions of everything 6 | RUN npm --quiet set progress=false \ 7 | && npm install --only=prod --no-optional \ 8 | && echo "Installed NPM packages:" \ 9 | && (npm list || true) \ 10 | && echo "Node.js version:" \ 11 | && node --version \ 12 | && echo "NPM version:" \ 13 | && npm --version 14 | 15 | COPY . ./ 16 | 17 | ENV APIFY_DISABLE_OUTDATED_WARNING=1 18 | -------------------------------------------------------------------------------- /cheerio-scraper/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /cheerio-scraper/apify.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cheerio-scraper", 3 | "version": "0.1", 4 | "buildTag": "latest", 5 | "env": null 6 | } 7 | -------------------------------------------------------------------------------- /cheerio-scraper/main.js: -------------------------------------------------------------------------------- 1 | const { runActor } = require('@apify/scraper-tools'); 2 | const CrawlerSetup = require('./src/crawler_setup'); 3 | 4 | runActor(CrawlerSetup); 5 | -------------------------------------------------------------------------------- /cheerio-scraper/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "actor-cheerio-scraper", 3 | "version": "2.0.0", 4 | "description": "Crawl web pages using HTTP requests and Cheerio", 5 | "main": "main.js", 6 | "dependencies": { 7 | "@apify/scraper-tools": "^0.4.4", 8 | "apify": "^2.3.2" 9 | }, 10 | "devDependencies": { 11 | "markdown-toc": "^1.2.0" 12 | }, 13 | "peerDependencies": { 14 | "cheerio": "^1.0.0-rc.3" 15 | }, 16 | "scripts": { 17 | "start": "node main.js", 18 | "build-toc": "./node_modules/.bin/markdown-toc README.md -i", 19 | "test": "nyc --reporter=html --reporter=text mocha --timeout 60000 --recursive", 20 | "lint": "eslint src test" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/apifytech/actor-scraper" 25 | }, 26 | "author": { 27 | "name": "Apify Technologies", 28 | "email": "support@apify.com", 29 | "url": "https://apify.com" 30 | }, 31 | "contributors": [ 32 | "Marek Trunkat ", 33 | "Ondra Urban " 34 | ], 35 | "license": "Apache-2.0", 36 | "homepage": "https://github.com/apifytech/actor-scraper" 37 | } 38 | -------------------------------------------------------------------------------- /docs/.eslintignore: -------------------------------------------------------------------------------- 1 | /all 2 | /web 3 | -------------------------------------------------------------------------------- /docs/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["@apify"] 3 | } 4 | -------------------------------------------------------------------------------- /docs/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | apify_storage 3 | node_modules 4 | .nyc_output 5 | coverage 6 | src/page_function.js 7 | .DS_Store 8 | -------------------------------------------------------------------------------- /docs/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/cheerio-scraper.md: -------------------------------------------------------------------------------- 1 | {{> intro}} 2 | 3 | {{> tools}} 4 | 5 | {{> build-page-function}} 6 | 7 | {{> title}} 8 | 9 | {{> description}} 10 | 11 | {{> modified-date}} 12 | 13 | {{> run-count}} 14 | 15 | {{> wrapping-it-up}} 16 | 17 | {{> pagination}} 18 | 19 | {{> downloading-scraped-data}} 20 | 21 | {{> bonus}} 22 | 23 | {{> final-word}} 24 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/bonus.js: -------------------------------------------------------------------------------- 1 | async function pageFunction(context) { 2 | switch (context.request.userData.label) { 3 | case 'START': return handleStart(context); 4 | case 'DETAIL': return handleDetail(context); 5 | } 6 | 7 | async function handleStart({ log, waitFor, $ }) { 8 | log.info('Store opened!'); 9 | 10 | const dataJson = $('#__NEXT_DATA__').html(); 11 | // We requested HTML, but the data are actually JSON. 12 | const data = JSON.parse(dataJson); 13 | 14 | for (const item of data.props.pageProps.items) { 15 | const { name, username } = item; 16 | const actorDetailUrl = `https://apify.com/${username}/${name}`; 17 | await context.enqueueRequest({ 18 | url: actorDetailUrl, 19 | userData: { 20 | label: 'DETAIL', 21 | }, 22 | }); 23 | } 24 | } 25 | 26 | async function handleDetail({ request, log, skipLinks, $ }) { 27 | const { url } = request; 28 | log.info(`Scraping ${url}`); 29 | await skipLinks(); 30 | 31 | // Do some scraping. 32 | const uniqueIdentifier = url 33 | .split('/') 34 | .slice(-2) 35 | .join('/'); 36 | 37 | return { 38 | url, 39 | uniqueIdentifier, 40 | title: $('header h1').text(), 41 | description: $('header span.actor-description').text(), 42 | modifiedDate: new Date( 43 | Number( 44 | $('ul.ActorHeader-stats time').attr('datetime'), 45 | ), 46 | ), 47 | runCount: Number( 48 | $('ul.ActorHeader-stats > li:nth-of-type(3)') 49 | .text() 50 | .match(/[\d,]+/)[0] 51 | .replace(/,/g, ''), 52 | ), 53 | }; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/description.js: -------------------------------------------------------------------------------- 1 | return { 2 | title: $('header h1').text(), 3 | description: $('header span.actor-description').text(), 4 | }; 5 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/modified-date.js: -------------------------------------------------------------------------------- 1 | return { 2 | title: $('header h1').text(), 3 | description: $('header span.actor-description').text(), 4 | modifiedDate: new Date( 5 | Number( 6 | $('ul.ActorHeader-stats time').attr('datetime'), 7 | ), 8 | ), 9 | }; 10 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/pagination.js: -------------------------------------------------------------------------------- 1 | async function pageFunction(context) { 2 | const { request, log, skipLinks, $ } = context; 3 | if (request.userData.label === 'START') { 4 | log.info('Store opened!'); 5 | 6 | const dataJson = $('#__NEXT_DATA__').html(); 7 | // We requested HTML, but the data are actually JSON. 8 | const data = JSON.parse(dataJson); 9 | 10 | for (const item of data.props.pageProps.items) { 11 | const { name, username } = item; 12 | const actorDetailUrl = `https://apify.com/${username}/${name}`; 13 | await context.enqueueRequest({ 14 | url: actorDetailUrl, 15 | userData: { 16 | label: 'DETAIL', 17 | }, 18 | }); 19 | } 20 | } 21 | if (request.userData.label === 'DETAIL') { 22 | const { url } = request; 23 | log.info(`Scraping ${url}`); 24 | await skipLinks(); 25 | 26 | // Do some scraping. 27 | const uniqueIdentifier = url 28 | .split('/') 29 | .slice(-2) 30 | .join('/'); 31 | 32 | return { 33 | url, 34 | uniqueIdentifier, 35 | title: $('header h1').text(), 36 | description: $('header span.actor-description').text(), 37 | modifiedDate: new Date( 38 | Number( 39 | $('ul.ActorHeader-stats time').attr('datetime'), 40 | ), 41 | ), 42 | runCount: Number( 43 | $('ul.ActorHeader-stats > li:nth-of-type(3)') 44 | .text() 45 | .match(/[\d,]+/)[0] 46 | .replace(/,/g, ''), 47 | ), 48 | }; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/run-count.js: -------------------------------------------------------------------------------- 1 | return { 2 | title: $('header h1').text(), 3 | description: $('header span.actor-description').text(), 4 | modifiedDate: new Date( 5 | Number( 6 | $('ul.ActorHeader-stats time').attr('datetime'), 7 | ), 8 | ), 9 | runCount: Number( 10 | $('ul.ActorHeader-stats > li:nth-of-type(3)') 11 | .text() 12 | .match(/[\d,]+/)[0] 13 | .replace(/,/g, ''), 14 | ), 15 | }; 16 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/title.js: -------------------------------------------------------------------------------- 1 | // Using Cheerio. 2 | return { 3 | title: $('header h1').text(), 4 | }; 5 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/wrapping-it-up-1.js: -------------------------------------------------------------------------------- 1 | const { url } = request; 2 | 3 | // ... 4 | 5 | const uniqueIdentifier = url 6 | .split('/') 7 | .slice(-2) 8 | .join('/'); 9 | 10 | return { 11 | url, 12 | uniqueIdentifier, 13 | title: $('header h1').text(), 14 | description: $('header span.actor-description').text(), 15 | modifiedDate: new Date( 16 | Number( 17 | $('ul.ActorHeader-stats time').attr('datetime'), 18 | ), 19 | ), 20 | runCount: Number( 21 | $('ul.ActorHeader-stats > li:nth-of-type(3)') 22 | .text() 23 | .match(/[\d,]+/)[0] 24 | .replace(/,/g, ''), 25 | ), 26 | }; 27 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/code/wrapping-it-up-2.js: -------------------------------------------------------------------------------- 1 | async function pageFunction(context) { 2 | // $ is Cheerio 3 | const { request, log, skipLinks, $ } = context; 4 | if (request.userData.label === 'START') { 5 | log.info('Store opened!'); 6 | // Do some stuff later. 7 | } 8 | if (request.userData.label === 'DETAIL') { 9 | const { url } = request; 10 | log.info(`Scraping ${url}`); 11 | await skipLinks(); 12 | 13 | // Do some scraping. 14 | const uniqueIdentifier = url 15 | .split('/') 16 | .slice(-2) 17 | .join('/'); 18 | 19 | return { 20 | url, 21 | uniqueIdentifier, 22 | title: $('header h1').text(), 23 | description: $('header span.actor-description').text(), 24 | modifiedDate: new Date( 25 | Number( 26 | $('ul.ActorHeader-stats time').attr('datetime'), 27 | ), 28 | ), 29 | runCount: Number( 30 | $('ul.ActorHeader-stats > li:nth-of-type(3)') 31 | .text() 32 | .match(/[\d,]+/)[0] 33 | .replace(/,/g, ''), 34 | ), 35 | }; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /docs/cheerio-scraper/partials/pagination.md: -------------------------------------------------------------------------------- 1 | ## [](#pagination) Pagination 2 | 3 | Pagination is just a term that represents "going to the next page of results". You may have noticed that we did not 4 | actually scrape all the actors, just the first page of results. That's because to load the rest of the actors, 5 | one needs to click the **Show more** button at the very bottom of the list. This is pagination. 6 | 7 | > This is a typical JavaScript pagination, sometimes called infinite scroll. Other pages may use links 8 | that take you to the next page. If you encounter those, just make a Pseudo URL for those links and they 9 | will be automatically enqueued to the request queue. Use a label to let the scraper know what kind of URL 10 | it's processing. 11 | 12 | If you paid close attention, you may now see a problem. How do we click a button in the page when we're working 13 | with Cheerio? We don't have a browser to do it and we only have the HTML of the page to work with. So the simple 14 | answer is that we can't click a button. Does that mean that we cannot get the data at all? Usually not, 15 | but it requires some clever DevTools-Fu. 16 | 17 | ### [](#analyzing-the-page) Analyzing the page 18 | 19 | While with Web Scraper and **Puppeteer Scraper** ([apify/puppeteer-scraper](https://apify.com/apify/puppeteer-scraper)), we could get away with simply clicking a button, 20 | with Cheerio Scraper we need to dig a little deeper into the page's architecture. For this, we will use 21 | the Network tab of the Chrome DevTools. 22 | 23 | > DevTools is a powerful tool with many features, so if you're not familiar with it, please [see Google's tutorial](https://developers.google.com/web/tools/chrome-devtools/network/), which explains everything much better than we ever could. 24 | 25 | We want to know what happens when we click the **Show more** button, so we open the DevTools **Network** tab and clear it. 26 | Then we click the **Show more** button and wait for incoming requests to appear in the list. 27 | 28 | ![Inspecting network in DevTools](../img/inspect-network.webp) 29 | 30 | Now, this is interesting. It seems that we've only received two images after clicking the button and no additional 31 | data. This means that the data about actors must already be available in the page and the **Show more** button only displays it. This is good news. 32 | 33 | ### [](#finding-the-actors) Finding the actors 34 | 35 | Now that we know the information we seek is already in the page, we just need to find it. The first actor in the store 36 | is Web Scraper, so let's try using the search tool in the **Elements** tab to find some reference to it. The first 37 | few hits do not provide any interesting information, but in the end, we find our goldmine. There is a `