Installations
npm install open-graph-scraper
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Min. Node Version
>=18.0.0
Node Version
18.18.0
NPM Version
9.8.1
Releases
Unable to fetch releases
Contributors
Languages
TypeScript (99.82%)
JavaScript (0.12%)
Dockerfile (0.06%)
Developer
jshemas
Download Statistics
Total Downloads
9,890,985
Last Day
4,704
Last Week
30,019
Last Month
214,691
Last Year
3,356,497
GitHub Statistics
690 Stars
1,202 Commits
108 Forks
10 Watching
3 Branches
40 Contributors
Package Meta Information
Latest Version
6.8.4
Package Id
open-graph-scraper@6.8.4
Unpacked Size
181.57 kB
Size
25.80 kB
File Count
35
NPM Version
9.8.1
Node Version
18.18.0
Publised On
14 Jan 2025
Total Downloads
Cumulative downloads
Total Downloads
9,890,985
Last day
-28.3%
4,704
Compared to previous day
Last week
-41%
30,019
Compared to previous week
Last month
-8.4%
214,691
Compared to previous month
Last year
52.6%
3,356,497
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
4
openGraphScraper
A simple node module(with TypeScript declarations) for scraping Open Graph and Twitter Card and other metadata off a site.
Note: open-graph-scraper
doesn't support browser usage at this time but you can use open-graph-scraper-lite
if you already have the HTML
and can't use Node's Fetch API.
Installation
1npm install open-graph-scraper --save
Usage
1const ogs = require('open-graph-scraper'); 2const options = { url: 'http://ogp.me/' }; 3ogs(options) 4 .then((data) => { 5 const { error, html, result, response } = data; 6 console.log('error:', error); // This returns true or false. True if there was an error. The error itself is inside the result object. 7 console.log('html:', html); // This contains the HTML of page 8 console.log('result:', result); // This contains all of the Open Graph results 9 console.log('response:', response); // This contains response from the Fetch API 10 })
Results JSON
Check the return for a success
flag. If success is set to true, then the url input was valid. Otherwise it will be set to false. The above example will return something like...
1{ 2 ogTitle: 'Open Graph protocol', 3 ogType: 'website', 4 ogUrl: 'https://ogp.me/', 5 ogDescription: 'The Open Graph protocol enables any web page to become a rich object in a social graph.', 6 ogImage: [ 7 { 8 height: '300', 9 type: 'image/png', 10 url: 'https://ogp.me/logo.png', 11 width: '300' 12 } 13 ], 14 charset: 'utf-8', 15 requestUrl: 'http://ogp.me/', 16 success: true 17}
Options
Name | Info | Default Value | Required |
---|---|---|---|
url | URL of the site. | x | |
html | You can pass in an HTML string to run ogs on it. (use without options.url) | ||
fetchOptions | Options that are used by the Fetch API | {} | |
timeout | Request timeout for Fetch (Default is 10 seconds) | 10 | |
blacklist | Pass in an array of sites you don't want ogs to run on. | [] | |
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 | |
customMetaTags | Here you can define custom meta tags you want to scrape. | [] | |
urlValidatorSettings | Sets the options used by validator.js for testing the URL | Here |
Note: open-graph-scraper
uses the Fetch API for requests and most of Fetch's options should work as open-graph-scraper
's fetchOptions
options.
Types And Import Example
1// example of how to get types 2import type { SuccessResult } from 'open-graph-scraper/types'; 3const example: SuccessResult = { 4 result: { ogTitle: 'this is a title' }, 5 error: false, 6 response: {}, 7 html: '<html></html>' 8} 9 10// import example 11import ogs from 'open-graph-scraper'; 12const options = { url: 'http://ogp.me/' }; 13ogs(options) 14 .then((data) => { 15 const { error, html, result, response } = data; 16 console.log('error:', error); // This returns true or false. True if there was an error. The error itself is inside the result object. 17 console.log('html:', html); // This contains the HTML of page 18 console.log('result:', result); // This contains all of the Open Graph results 19 console.log('response:', response); // This contains response from the Fetch API 20 });
Custom Meta Tag Example
1const ogs = require('open-graph-scraper'); 2const options = { 3 url: 'https://github.com/jshemas/openGraphScraper', 4 customMetaTags: [{ 5 multiple: false, // is there more than one of these tags on a page (normally this is false) 6 property: 'hostname', // meta tag name/property attribute 7 fieldName: 'hostnameMetaTag', // name of the result variable 8 }], 9}; 10ogs(options) 11 .then((data) => { 12 const { result } = data; 13 console.log('hostnameMetaTag:', result.customMetaTags.hostnameMetaTag); // hostnameMetaTag: github.com 14 })
HTML Example
1const ogs = require('open-graph-scraper'); 2const options = { 3 html: `<html><head> 4 <link rel="icon" type="image/png" href="https://bar.com/foo.png" /> 5 <meta charset="utf-8" /> 6 <meta property="og:description" name="og:description" content="html description example" /> 7 <meta property="og:image" name="og:image" content="https://www.foo.com/bar.jpg" /> 8 <meta property="og:title" name="og:title" content="foobar" /> 9 <meta property="og:type" name="og:type" content="website" /> 10 </head></html>` 11}; 12ogs(options) 13 .then((data) => { 14 const { result } = data; 15 console.log('result:', result); 16 // result: { 17 // ogDescription: 'html description example', 18 // ogTitle: 'foobar', 19 // ogType: 'website', 20 // ogImage: [ { url: 'https://www.foo.com/bar.jpg', type: 'jpg' } ], 21 // favicon: 'https://bar.com/foo.png', 22 // charset: 'utf-8', 23 // success: true 24 // } 25 }) 26
User Agent Example
The request header is set to undici by default. Some sites might block this, and changing the userAgent
might work. If not you can try using a proxy for the request and then pass the html
into open-graph-scraper
.
1const ogs = require("open-graph-scraper"); 2const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36'; 3ogs({ url: 'https://www.wikipedia.org/', fetchOptions: { headers: { 'user-agent': userAgent } } }) 4 .then((data) => { 5 const { error, html, result, response } = data; 6 console.log('error:', error); // This returns true or false. True if there was an error. The error itself is inside the result object. 7 console.log('html:', html); // This contains the HTML of page 8 console.log('result:', result); // This contains all of the Open Graph results 9 console.log('response:', response); // This contains response from the Fetch API 10 })
Running the example app
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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
9 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 9
Reason
SAST tool is not run on all commits -- score normalized to 7
Details
- Warn: 18 commits out of 24 are checked with a SAST tool
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/jshemas/openGraphScraper/node.js.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.js.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/jshemas/openGraphScraper/node.js.yml/master?enable=pin
- Warn: containerImage not pinned by hash: example/Dockerfile:4: pin your Docker image by updating node:20 to node:20@sha256:d17aaa2a2fd82e09bd6a6da7cc4a79741340d2a3e39d172d1b30f295b1a850ff
- Warn: npmCommand not pinned by hash: example/Dockerfile:10
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 containerImage dependencies pinned
- Info: 1 out of 2 npmCommand dependencies pinned
Reason
Found 2/11 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/node.js.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Score
5.4
/10
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to open-graph-scraper
@types/open-graph-scraper
TypeScript definitions for open-graph-scraper
open-graph-scraper-lite
Javascript scraper module for Open Graph and Twitter Card info
metadata-scraper
A Javascript library for scraping/parsing metadata from a web page.
react-native-open-graph-scraper
React Native scraper module for Open Graph and Twitter Card info