ns-rss-spider 0.0.30 → 0.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +0 -3
- package/dist/cjs/parsers/36kr.js +26 -18
- package/dist/cjs/utils/browser.d.ts +28 -2
- package/dist/cjs/utils/browser.js +39 -5
- package/package.json +1 -1
package/dist/cjs/index.d.ts
CHANGED
package/dist/cjs/index.js
CHANGED
|
@@ -19,7 +19,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/index.ts
|
|
20
20
|
var src_exports = {};
|
|
21
21
|
__export(src_exports, {
|
|
22
|
-
getWebContent: () => import_browser.getWebContent,
|
|
23
22
|
parseContent: () => import_parseContent.parseContent,
|
|
24
23
|
parseRss: () => import_parse.parseRss,
|
|
25
24
|
strategies: () => import_strategy.strategies
|
|
@@ -28,10 +27,8 @@ module.exports = __toCommonJS(src_exports);
|
|
|
28
27
|
var import_parse = require("./parse");
|
|
29
28
|
var import_strategy = require("./strategy");
|
|
30
29
|
var import_parseContent = require("./parseContent");
|
|
31
|
-
var import_browser = require("./utils/browser");
|
|
32
30
|
// Annotate the CommonJS export names for ESM import in node:
|
|
33
31
|
0 && (module.exports = {
|
|
34
|
-
getWebContent,
|
|
35
32
|
parseContent,
|
|
36
33
|
parseRss,
|
|
37
34
|
strategies
|
package/dist/cjs/parsers/36kr.js
CHANGED
|
@@ -23,34 +23,42 @@ __export(kr_exports, {
|
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(kr_exports);
|
|
25
25
|
var import_zx = require("zx");
|
|
26
|
-
var import_cheerio = require("cheerio");
|
|
27
26
|
var import_constants = require("../utils/constants");
|
|
28
27
|
var import_browser = require("../utils/browser");
|
|
29
28
|
var _36kr = {
|
|
30
29
|
parse: true,
|
|
31
30
|
getThumbs: async (items) => {
|
|
31
|
+
var _a, _b, _c;
|
|
32
32
|
const url = "https://m.36kr.com/";
|
|
33
|
-
const
|
|
33
|
+
const initialState = await (0, import_browser.runInPage)(url, async (page) => {
|
|
34
|
+
const data = await page.evaluate(async () => window.initialState);
|
|
35
|
+
return data;
|
|
36
|
+
}, {
|
|
34
37
|
userAgent: import_constants.iosUA
|
|
35
38
|
}).catch((e) => {
|
|
36
39
|
console.error(import_zx.chalk.red(`获取 ${url} 错误`), e);
|
|
37
40
|
});
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
41
|
+
console.log("initialState", initialState);
|
|
42
|
+
if ((_c = (_b = (_a = initialState == null ? void 0 : initialState.home) == null ? void 0 : _a.flow) == null ? void 0 : _b.itemList) == null ? void 0 : _c.length) {
|
|
43
|
+
const idThumbKv = initialState.home.flow.itemList.reduce((prev, item) => {
|
|
44
|
+
var _a2, _b2;
|
|
45
|
+
if ((item == null ? void 0 : item.itemId) && ((_a2 = item.templateMaterial) == null ? void 0 : _a2.widgetImage)) {
|
|
46
|
+
prev[item.itemId] = (_b2 = item.templateMaterial) == null ? void 0 : _b2.widgetImage;
|
|
47
|
+
}
|
|
48
|
+
return prev;
|
|
49
|
+
}, {});
|
|
50
|
+
const kv = {};
|
|
51
|
+
Object.keys(idThumbKv).forEach((articleId) => {
|
|
52
|
+
var _a2;
|
|
53
|
+
const thumb = idThumbKv[articleId];
|
|
54
|
+
const guid = (_a2 = items.find((a) => (a.guid || a.link || "").includes(`/p/${articleId}`))) == null ? void 0 : _a2.guid;
|
|
55
|
+
if (guid && thumb) {
|
|
56
|
+
kv[guid] = thumb;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return kv;
|
|
60
|
+
}
|
|
61
|
+
return void 0;
|
|
54
62
|
}
|
|
55
63
|
};
|
|
56
64
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
import { Page } from "playwright";
|
|
2
|
+
type RunOptions = {
|
|
2
3
|
userAgent?: string;
|
|
3
|
-
}
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* 在 url 中执行 fn,并获取 fn 的结果
|
|
7
|
+
* @param url
|
|
8
|
+
* @param fn
|
|
9
|
+
* @param options
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare function runInPage<T>(url: string, fn: (page: Page) => Promise<T>, options?: RunOptions): Promise<T>;
|
|
13
|
+
/**
|
|
14
|
+
* 根据 url 获取 content.
|
|
15
|
+
*
|
|
16
|
+
* 1. 支持获取前滚动页面
|
|
17
|
+
*
|
|
18
|
+
* @param url
|
|
19
|
+
* @param options
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export declare function getPageHtml(url: string, options?: {
|
|
23
|
+
scroll?: {
|
|
24
|
+
offset?: number;
|
|
25
|
+
interval?: number;
|
|
26
|
+
};
|
|
27
|
+
} & RunOptions): Promise<string>;
|
|
28
|
+
export declare function scrollToBottom(page: Page, offset?: number, interval?: number): Promise<void>;
|
|
29
|
+
export {};
|
|
@@ -19,11 +19,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
// src/utils/browser.ts
|
|
20
20
|
var browser_exports = {};
|
|
21
21
|
__export(browser_exports, {
|
|
22
|
-
|
|
22
|
+
getPageHtml: () => getPageHtml,
|
|
23
|
+
runInPage: () => runInPage,
|
|
24
|
+
scrollToBottom: () => scrollToBottom
|
|
23
25
|
});
|
|
24
26
|
module.exports = __toCommonJS(browser_exports);
|
|
25
27
|
var import_playwright = require("playwright");
|
|
26
|
-
async function
|
|
28
|
+
async function runInPage(url, fn, options) {
|
|
27
29
|
const browser = await import_playwright.chromium.launch();
|
|
28
30
|
const page = await browser.newPage({
|
|
29
31
|
userAgent: options == null ? void 0 : options.userAgent
|
|
@@ -32,15 +34,47 @@ async function getWebContent(url, options) {
|
|
|
32
34
|
const waitForLoadPromise = new Promise((r) => {
|
|
33
35
|
resolve = r;
|
|
34
36
|
});
|
|
35
|
-
page.on("load", (page2) => {
|
|
36
|
-
|
|
37
|
+
page.on("load", async (page2) => {
|
|
38
|
+
const r = await fn(page2);
|
|
39
|
+
resolve == null ? void 0 : resolve(r);
|
|
37
40
|
});
|
|
38
41
|
await page.goto(url);
|
|
39
42
|
const content = await waitForLoadPromise;
|
|
40
43
|
await browser.close();
|
|
41
44
|
return content;
|
|
42
45
|
}
|
|
46
|
+
async function getPageHtml(url, options) {
|
|
47
|
+
return await runInPage(
|
|
48
|
+
url,
|
|
49
|
+
async (page) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
if (options == null ? void 0 : options.scroll) {
|
|
52
|
+
await scrollToBottom(
|
|
53
|
+
page,
|
|
54
|
+
(_a = options == null ? void 0 : options.scroll) == null ? void 0 : _a.offset,
|
|
55
|
+
(_b = options == null ? void 0 : options.scroll) == null ? void 0 : _b.interval
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const html = await page.locator("html").innerHTML();
|
|
59
|
+
return html;
|
|
60
|
+
},
|
|
61
|
+
options
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
async function scrollToBottom(page, offset = 300, interval = 10) {
|
|
65
|
+
const height = await page.evaluate(async () => {
|
|
66
|
+
const height2 = document.body.scrollHeight;
|
|
67
|
+
return height2;
|
|
68
|
+
});
|
|
69
|
+
console.log("height", height);
|
|
70
|
+
for (let i = 0; i < height; i += offset) {
|
|
71
|
+
page.mouse.wheel(0, i * offset);
|
|
72
|
+
await page.waitForTimeout(interval);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
43
75
|
// Annotate the CommonJS export names for ESM import in node:
|
|
44
76
|
0 && (module.exports = {
|
|
45
|
-
|
|
77
|
+
getPageHtml,
|
|
78
|
+
runInPage,
|
|
79
|
+
scrollToBottom
|
|
46
80
|
});
|