-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearchEngines.js
192 lines (184 loc) · 6.17 KB
/
searchEngines.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const puppeteer = require("puppeteer");
async function googleSearch(query) {
try {
//https://serpapi.com/search-api
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
`https://www.google.com.hk/search?q=${encodeURIComponent(
query
)}&oq=${encodeURIComponent(
query
)}&uule=w+CAIQICIaQXVzdGluLFRleGFzLFVuaXRlZCBTdGF0ZXM&hl=en&gl=us&sourceid=chrome&ie=UTF-8%22#ip=1`
);
const summaries = await page.evaluate(() => {
const liElements = Array.from(
document.querySelector("#search > div > div").childNodes
);
const firstFiveLiElements = liElements.slice(0, 5);
return firstFiveLiElements.map((li) => {
const linkElement = li.querySelector("a");
const href = linkElement.getAttribute("href");
const title = linkElement.querySelector("a > h3").textContent;
const abstract = Array.from(
li.querySelectorAll("div > div > div > div > div > div > span")
)
.map((e) => e.textContent)
.join("");
return { href, title, abstract };
});
});
await browser.close();
console.log(summaries);
return summaries;
} catch (error) {
console.error("An error occurred:", error);
}
}
async function bingSearch(query) {
try {
//https://serpapi.com/bing-search-api
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox']
});
const page = await browser.newPage();
await page.goto(
`https://cn.bing.com/search?mkt=zh-cn&FORM=BEHPTB&q=${encodeURIComponent(
query
)}`
);
const summaries = await page.evaluate(() => {
const liElements = Array.from(
document.querySelectorAll("#b_results > .b_algo")
);
console.log(liElements)
const firstFiveLiElements = liElements.slice(0, 5);
return firstFiveLiElements.map((li) => {
const linkElement = li.querySelector("a");
const href = linkElement.getAttribute("href");
const title = li.querySelector("h2").textContent;
const abstractEle = li.querySelector('div > p');
let abstract = abstractEle ? abstractEle.textContent : "";
if (abstract.length > 3) {
abstract = abstract.substring(3, abstract.length);
}
return { href, title , abstract};
});
});
await browser.close();
console.log(summaries);
return summaries;
} catch (error) {
console.error("An error occurred:", error);
}
}
async function baiduSearch(query) {
try {
//https://serpapi.com/bing-search-api
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox']
});
const page = await browser.newPage();
console.log(`https://www.baidu.com/s?wd=${encodeURIComponent(
query
)}`);
await page.goto(
`https://www.baidu.com/s?wd=${encodeURIComponent(
query
)}`
);
const summaries = await page.evaluate(() => {
const contentDivEle = document.querySelector("#content_left");
const firstFiveDivElements = Array.from(contentDivEle.querySelectorAll("div[class='c-container']")).slice(0, 5); // trick
// const firstFiveLiElements = liElements.slice(0, 5);
return firstFiveDivElements.map((div) => {
const linkElement = div.querySelector('h3').querySelector('a');
const href = linkElement.getAttribute("href");
const title = linkElement.textContent;
const abstractEle = div.querySelector("span[class^='content-right']");
let abstract = abstractEle ? abstractEle.textContent : "";
if (abstract.length > 3) {
abstract = abstract.substring(3, abstract.length);
}
return { href, title , abstract};
});
});
await browser.close();
console.log(summaries);
return summaries;
} catch (error) {
console.error("An error occurred:", error);
}
}
async function yahooSearch(query) {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
`https://search.yahoo.com/search?p=${encodeURIComponent(
query
)}&ei=UTF-8&fr=fp-tts`
);
const summaries = await page.evaluate(() => {
const liElements = Array.from(
document.querySelector(".searchCenterMiddle").childNodes
);
const firstFiveLiElements = liElements.slice(0, 5);
return firstFiveLiElements.map((li) => {
const compTextElement = li.querySelector(".compText");
const linkElement = li.querySelector("a");
const href = linkElement.getAttribute("href");
const title = linkElement.getAttribute("aria-label");
const abstract = compTextElement ? compTextElement.textContent : "";
return { href, title, abstract };
});
});
await browser.close();
return summaries;
} catch (error) {
console.error("An error occurred:", error);
}
}
async function duckduckgoSearch(query) {
try {
//https://serpapi.com/duckduckgo-search-api
// 可以改区域,这些设置的是港区
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
`https://duckduckgo.com/?q=${encodeURIComponent(query)}&kl=hk-tzh&ia=web`
);
const summaries = await page.evaluate(() => {
const liElements = Array.from(
document.querySelectorAll("#react-layout ol li")
);
const firstFiveLiElements = liElements.slice(0, 5);
return firstFiveLiElements.map((li) => {
const abstractElement = li
.querySelector("div:nth-child(3)")
.querySelector("div");
const linkElement = li
.querySelector("div:nth-child(2)")
.querySelector("a");
const href = linkElement.getAttribute("href");
const title = linkElement.textContent;
const abstract = abstractElement ? abstractElement.textContent : "";
return { href, title, abstract };
});
});
await browser.close();
console.log(summaries);
return summaries;
} catch (error) {
console.error("An error occurred:", error);
}
}
module.exports = {
googleSearch,
bingSearch,
yahooSearch,
duckduckgoSearch,
baiduSearch
};