本帖最后由 genkangyi 于 2021-9-11 17:09 编辑
node.js 安装环境我就不描述了
需要安装 selenium-webdriver chromedriver
执行下面语句安装
[JavaScript] 纯文本查看 复制代码 npm install selenium-webdriver
npm install chromedriver
[JavaScript] 纯文本查看 复制代码 const chromedriver = require('chromedriver');
const {
Builder,
By,
Key,
until
} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const fs = require('fs');
fs.writeFile('D:/paqu.txt', '', err => {})
let driver = new Builder().forBrowser('chrome').build();
(async function start() {
let url = `` //url
await driver.get(url);
getData()
})();
async function getData(){
let items = await driver.findElements(By.css("tbody[id*='normalthread']"))
for(let i in items){
let results = []
let item = items[i]
let focus = await item.findElement(By.css("tr > th > em > a")).getText()
let title = await item.findElement(By.css(".s.xst")).getText()
let time = await item.findElement(By.css("cite+em>a")).getText()
let reg = /\[{"(.*)"}\]/g
console.log(focus, title, time)
results.push({focus, title, time})
let re = JSON.stringify(results).replace(reg,"$1")
fs.writeFile('D:/paqu.txt', `\r\n${re}`, {flag:'a'}, err => {
if (err) throw err
})
}
}
欢迎讨论! |