Skip to content

Commit 54caf26

Browse files
committed
Update README
1 parent f7bc443 commit 54caf26

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ Check the return for a ```success``` flag. If success is set to true, then the u
5454

5555
## Options
5656

57-
| Name | Info | Default Value | Required |
58-
|----------------------|----------------------------------------------------------------------------|---------------|----------|
59-
| url | URL of the site. | | x |
60-
| html | You can pass in an HTML string to run ogs on it. (use without options.url) | | |
61-
| fetchOptions | Options that are used by the Fetch API | {} | |
62-
| timeout | Request timeout for Fetch (Default is 10 seconds) | 10 | |
63-
| blacklist | Pass in an array of sites you don't want ogs to run on. | [] | |
64-
| onlyGetOpenGraphInfo | Only fetch open graph info and don't fall back on anything else. Also accepts an array of properties for which no fallback should be used | false | |
65-
| customMetaTags | Here you can define custom meta tags you want to scrape. | [] | |
66-
| urlValidatorSettings | Sets the options used by validator.js for testing the URL | [Here](https://github.com/jshemas/openGraphScraper/blob/master/lib/utils.ts#L4-L17) | |
57+
| Name | Info | Default Value | Required |
58+
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|----------|
59+
| url | URL of the site. | | x |
60+
| html | You can pass in an HTML string to run ogs on it. (use without options.url) | | |
61+
| fetchOptions | Options that are used by the Fetch API | {} | |
62+
| timeout | Request timeout for Fetch (Default is 10 seconds) | 10 | |
63+
| blacklist | Pass in an array of sites you don't want ogs to run on. | [] | |
64+
| onlyGetOpenGraphInfo | Only fetch open graph info and don't fall back on anything else. Also accepts an array of properties for which no fallback should be used | false | |
65+
| customMetaTags | Here you can define custom meta tags you want to scrape. | [] | |
66+
| urlValidatorSettings | Sets the options used by validator.js for testing the URL | [Here](https://github.com/jshemas/openGraphScraper/blob/master/lib/utils.ts#L4-L17) | |
67+
| jsonLDOptions | Sets the options used when parsing JSON-LD data | | |
6768

6869
Note: `open-graph-scraper` uses the [Fetch API](https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch) for requests and most of [Fetch's options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) should work as `open-graph-scraper`'s `fetchOptions` options.
6970

@@ -159,6 +160,26 @@ ogs({ url: 'https://www.wikipedia.org/', fetchOptions: { headers: { 'user-agent'
159160
})
160161
```
161162

163+
## JSON-LD Parsing Options Example
164+
165+
`throwOnJSONParseError` and `logOnJSONParseError` properties control what happens if `JSON.parse`
166+
throws an error when parsing JSON-LD data.
167+
If `throwOnJSONParseError` is set to `true`, then the error will be thrown.
168+
If `logOnJSONParseError` is set to `true`, then the error will be logged to the console.
169+
170+
```javascript
171+
const ogs = require("open-graph-scraper");
172+
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36';
173+
ogs({ url: 'https://www.wikipedia.org/', jsonLDOptions: { throwOnJSONParseError: true } })
174+
.then((data) => {
175+
const { error, html, result, response } = data;
176+
console.log('error:', error); // This returns true or false. True if there was an error. The error itself is inside the result object.
177+
console.log('html:', html); // This contains the HTML of page
178+
console.log('result:', result); // This contains all of the Open Graph results
179+
console.log('response:', response); // This contains response from the Fetch API
180+
})
181+
```
182+
162183
## Running the example app
163184

164185
Inside the `example` folder contains a simple express app where you can run `npm ci && npm run start` to spin up. Once the app is running, open a web browser and go to `http://localhost:3000/scraper?url=http://ogp.me/` to test it out. There is also a `Dockerfile` if you want to run this example app in a docker container.

0 commit comments

Comments
 (0)