-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrap.js
More file actions
165 lines (137 loc) · 4.44 KB
/
Copy pathscrap.js
File metadata and controls
165 lines (137 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as puppeteer from 'puppeteer';
import * as cheerio from 'cheerio';
import { sleep, parseDate } from './util.js';
export async function scrapeNewsList(url, source) {
if(source === 'CryptoNews'){
return await scrapeNewsListCN(url);
} else if(source.includes('CT-')){
return await scrapeNewsListCT(url);
}
return null;
}
export async function scrapeNewsItem(url, source) {
if(source === 'CryptoNews'){
return await scrapeNewsItemCN(url);
} else if(source.includes('CT-')){
return await scrapeNewsItemCT(url);
}
return null;
}
async function scrapeNewsListCN(url) {
const $ = await readUrl(url);
if(!$){
return null;
}
const results = [];
$('div.post-loop').each((index, row) => {
const link = $(row).find('a.post-loop__link')[0];
const meta = $(row).find('time.post-loop__date')[0];
if(link && meta){
const item = { title: $(link).text().trim(), time: $(meta).attr('datetime'), url: $(link).attr('href') };
item.timeObj = parseDate(item.time);
results.push(item);
}
});
return results;
}
async function scrapeNewsListCT(url) {
const $ = await readUrl(url, 'div.markets-header', false);
if(!$){
return null;
}
const results = [];
$('article.post-card-inline').each((index, row) => {
const link = $(row).find('a.post-card-inline__figure-link')[0];
const title = $(row).find('span.post-card-inline__title')[0];
const time = $(row).find('time.post-card-inline__date')[0];
if(link && title && time){
const item = { title: $(title).text().trim(), time: /*$(time).attr('datetime') + ' ' + */$(time).text().trim(), url: 'https://cointelegraph.com' + $(link).attr('href') };
item.timeObj = parseDate(item.time);
results.push(item);
}
});
return results;
}
async function scrapeNewsItemCN(url) {
const $ = await readUrl(url);
if(!$){
return null;
}
const article = $('article.post-detail')[0];
if(article){
const result = {};
const author = $(article).find('a.author-list__link')[0];
const editor = $(article).find('a.author-list__link')[1];
if(author){
result.author = $(author).text().trim();
}
if(editor){
result.editor = $(editor).text().trim();
}
const content = $(article).find('div.post-detail__content.blocks')[0];
if(content){
$(content).find('div.wp-block-rank-math-toc-block').remove();
$(content).find('figure.wp-block-image').remove();
$(content).find('div.cn-block-related-link').remove();
$(content).find('h2#fa-qs').remove();
$(content).find('div.rank-math-block').remove();
// result.contentOrg = content;
// result.contentHTML = $(content).html();
result.content = $(content).text().replace(/\\[nrtbf]|[\s]+/g, ' ').trim();
}
return result;
}
return null;
}
async function scrapeNewsItemCT(url) {
const $ = await readUrl(url, 'aa', false);
if(!$){
return null;
}
const article = $('article.post__article')[0];
if(article){
const result = {};
const author = $(article).find('a.post-meta__author-link')[0];
if(author){
result.author = $(author).text().trim();
}
const content = $(article).find('div.post__content-wrapper div.post-content')[0];
if(content){
$(content).find('figure').remove();
$(content).find('style').remove();
$(content).find('a.text-banner').remove();
$(content).find('button[data-testid="close-ad-button"]').remove();
$(content).find('img').remove();
$(content).find('div.cn-block-related-link').remove();
$(content).find('form').remove();
// result.contentOrg = content;
// result.contentHTML = $(content).html();
result.content = $(content).text().replace(/\\[nrtbf]|[\s]+/g, ' ').trim();
}
return result;
}
return null;
}
async function readUrl(url, selectorToWait = null, launchHeadless = true){
let $;
let browser;
try {
browser = await puppeteer.launch({
headless: launchHeadless, // Run in headless mode
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
if(selectorToWait){
// await page.waitForSelector(selectorToWait);
await sleep(5000); // Sleep for 3 seconds
}
const html = await page.content();
// console.log(html);
$ = cheerio.load(html);
} catch (error) {
console.error('Error scraping the table:', error);
} finally {
if (browser) await browser.close();
}
return $;
}