代码
// get_url.js
const process = require('process');
const puppeteer = require('puppeteer');
//npm install puppeteer
//访问代理,如果是在国内跑,一般要加上代理
const PROXY = "socks5://127.0.0.1:1080"
//页面加载后的等待时间,根据网速来设定,如果网速慢,就多等待一会
const SLEEP = 5000
//延时函数(此延时非彼延时!)
function sleep(delay) {
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
resolve(1)
} catch (e) {
reject(0)
}
}, delay)
})
}
async function view(url,proxy=null){
console.log(`URL:\t${url}`)
if(proxy)
console.log(`PROXY:\t${proxy}`)
const browser = await puppeteer.launch({
headless: true,
ignoreHTTPSErrors: true,
defaultViewport: {width: 1920, height: 1080},
args: [`--proxy-server=${proxy}`,'--no-sandbox']
});
const page = await browser.newPage()
await page.setViewport({width: 1920, height: 1080})
// set page event hook
page.on('request', request => {
if ( request.resourceType() == "media" && request.url().startsWith("https://video.twimg.com/") )
console.log( "REQUEST\t" + request.url() )
});
await page.goto(url)
await page.click('.r-1ndi9ce > div:nth-child(1) > div:nth-child(1) > span:nth-child(1) > span:nth-child(1)');
await sleep(SLEEP)
await page.screenshot({ path: 'debug.png' })
await browser.close()
}
(async () => {
let proxy = PROXY
let arguments = process.argv.splice(2)
let url = arguments[0]
await view( url,proxy )
})();
用法
node get_url.js TWEETS_URL
测试用例:
node get_url.js https://twitter.com/lyf112211/status/1433657754162323457?s=05
如果觉得对你有帮助,希望去知乎帮点个赞。
1
mooniitt 2021-09-09 22:25:14 +08:00
真的是片!
|