-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #390 from extractus/8.0.9
v8.0.9
Showing
11 changed files
with
115 additions
and
26 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
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 |
---|---|---|
|
@@ -8,6 +8,6 @@ | |
}, | ||
"dependencies": { | ||
"@extractus/article-extractor": "latest", | ||
"express": "^4.18.2" | ||
"express": "latest" | ||
} | ||
} |
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,19 @@ | ||
# node-article-parser with Pupperteer | ||
|
||
Install dependencies: | ||
|
||
```bash | ||
npm i | ||
|
||
# or pnpm, yarn | ||
``` | ||
|
||
Start server: | ||
|
||
```bash | ||
npm start | ||
``` | ||
|
||
Open `http://localhost:3100/?url=https://client-side-rendering.pages.dev/lorem-ipsum` to see the result. | ||
|
||
--- |
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,64 @@ | ||
import puppeteer from 'puppeteer' | ||
import express from 'express' | ||
import { extractFromHtml } from '@extractus/article-extractor' | ||
|
||
const app = express() | ||
|
||
const meta = { | ||
service: 'article-parser-pupperteer', | ||
lang: 'javascript', | ||
server: 'express', | ||
platform: 'node', | ||
} | ||
|
||
const loadHtml = async (url) => { | ||
let browser = null | ||
try { | ||
console.log('Initialize puppeteer engine') | ||
browser = await puppeteer.launch() | ||
const page = await browser.newPage() | ||
await page.setDefaultNavigationTimeout(6e4) | ||
console.log(`Start rendering target page "${url}"`) | ||
await page.goto(url, { | ||
waitUntil: 'networkidle0', | ||
}) | ||
console.log(`Load html content from target page ${url}`) | ||
const html = await page.content() | ||
return html | ||
} catch (err) { | ||
console.error(err) | ||
return null | ||
} finally { | ||
if (browser) { | ||
await browser.close() | ||
} | ||
} | ||
} | ||
|
||
app.get('/', async (req, res) => { | ||
const url = req.query.url | ||
if (!url) { | ||
return res.json(meta) | ||
} | ||
try { | ||
const html = await loadHtml(url) | ||
const data = await extractFromHtml(html, url) | ||
return res.json({ | ||
error: 0, | ||
message: 'article has been extracted successfully', | ||
data, | ||
meta, | ||
}) | ||
} catch (err) { | ||
return res.json({ | ||
error: 1, | ||
message: err.message, | ||
data: null, | ||
meta, | ||
}) | ||
} | ||
}) | ||
|
||
app.listen(3100, () => { | ||
console.log('Server is running at http://localhost:3100') | ||
}) |
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,14 @@ | ||
{ | ||
"name": "node-pupperteer", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@extractus/article-extractor": "latest", | ||
"express": "latest", | ||
"puppeteer": "latest" | ||
} | ||
} |
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