-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
duty-machine
committed
Dec 4, 2020
1 parent
2647cd2
commit 91524e2
Showing
1 changed file
with
36 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
let { URL } = require('url') | ||
let fetch = require('node-fetch') | ||
let { JSDOM } = require('jsdom') | ||
|
||
module.exports = { | ||
test(url) { | ||
let parsed = new URL(url) | ||
return parsed.hostname == 'www.zhihu.com' | ||
}, | ||
|
||
async process(url) { | ||
let res = await fetch(url, { | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:74.0) Gecko/20100101 Firefox/74.0' | ||
} | ||
}) | ||
let html = await res.text() | ||
let document = new JSDOM(html).window.document | ||
|
||
let title = document.querySelector('h1.QuestionHeader-title').textContent | ||
let author = document.querySelector('.AnswerCard meta[itemProp="name"]').content | ||
let content = document.querySelector('.AnswerCard .RichContent-inner') | ||
|
||
return { | ||
title, | ||
author, | ||
dom: content | ||
} | ||
|
||
}, | ||
|
||
samples: [ | ||
'https://www.zhihu.com/question/32059030/answer/1541280721' | ||
] | ||
|
||
} |