-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support setup headless and HTTP requests. Introduce SPA Engine.…
… Improve typings. Update docs.
- Loading branch information
1 parent
1f34382
commit 4eb708c
Showing
22 changed files
with
251 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# scrapeSpa | ||
|
||
The `scrapeSpa` function takes an page URL and an object containing `selectors` as input and returns an object with data extracted from the HTML string. It uses the `puppeteer` and `utils/spa` to load the HTML and extract the required information from single-page applications (SPAs) that load content dynamically. | ||
|
||
## Selectors | ||
|
||
The `selectors` should be an object with keys that correspond to the data that you want to extract, and values that are the corresponding CSS selectors and HTML tag attributes. | ||
|
||
## Request | ||
|
||
The `request` configuration should be an object that correspond to the `LaunchOptions` type from `puppeeter`. Enjoy this flexibility. | ||
|
||
## Example | ||
|
||
```javascript | ||
import { scrapeSpa } from "tatooine" | ||
|
||
const htmlUrl = "https://example.com" | ||
const htmlData = await scrapeSpa(htmlUrl, { | ||
selectors: { | ||
title: { selector: "title" }, | ||
featured: { selector: "img", attribute: "src" }, | ||
}, | ||
request: { | ||
headless: true, | ||
args: ["--no-sandbox"], | ||
executablePath: "/opt/homebrew/bin/chromium", | ||
}, | ||
}) | ||
|
||
// Output: | ||
// { | ||
// title: 'My Title', | ||
// heading: 'Hello World!', | ||
// } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { EngineType } from "../../lib/types" | ||
|
||
const SpaScrapingFixture = { | ||
url: "https://davidwalsh.name/demo/lazyload-2.0.php", | ||
engine: EngineType.Spa, | ||
options: { | ||
selectors: { | ||
title: { | ||
selector: ".demo-wrapper table tr .image img", | ||
attribute: "src", | ||
}, | ||
}, | ||
request: { | ||
executablePath: "/opt/homebrew/bin/chromium", | ||
}, | ||
}, | ||
} | ||
|
||
// TODO: Create Fixtures with Invalid Selectors | ||
|
||
export { SpaScrapingFixture } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const DEFAULT_HTTP_REQUESTS_OPTIONS = { | ||
headers: { | ||
"Cache-Control": "no-cache", | ||
"Accept-Encoding": "gzip, deflate", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as scrape } from "./default" | ||
export { default as scrapeHtml } from "./html" | ||
export { default as scrapeSpa } from "./spa" | ||
export { default as scrapeXml } from "./xml" | ||
export { default as scrapeJson } from "./json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./spa" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { LaunchOptions } from "puppeteer" | ||
import { JSDOM } from "jsdom" | ||
import { TScrapedDataPromise, IScrapeSpaOptions } from "../../types" | ||
import fetchSpa from "../../utils/request/spa" | ||
import extractData from "../../utils/extract/html" | ||
|
||
const processData = async ( | ||
url: string, | ||
options: IScrapeSpaOptions | ||
): TScrapedDataPromise => { | ||
const { selectors, request } = options | ||
|
||
const htmlContent = await fetchSpa(url, request as LaunchOptions) | ||
const dom = new JSDOM(htmlContent) | ||
const document = dom.window.document | ||
return extractData(document, selectors) | ||
} | ||
|
||
const scrapeSpa = async ( | ||
url: string, | ||
{ selectors, request }: IScrapeSpaOptions | ||
): TScrapedDataPromise => { | ||
const data = await processData(url, { selectors, request }) | ||
return data | ||
} | ||
|
||
export default scrapeSpa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.