You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.5 KiB
52 lines
1.5 KiB
1 year ago
|
import Hero from '@ulixee/hero';
|
||
|
|
||
|
class Douyin {
|
||
|
constructor(heroCloudServer) {
|
||
|
this.heroServer = heroCloudServer ? heroCloudServer : '';
|
||
|
}
|
||
|
|
||
|
async scrap(url) {
|
||
|
let data = {};
|
||
|
|
||
|
try {
|
||
|
let options = {};
|
||
|
if (this.heroServer) {
|
||
|
options.connectionToCore = this.heroServer;
|
||
|
}
|
||
|
|
||
|
const hero = new Hero(options);
|
||
|
await hero.goto(url, {
|
||
|
timeoutMs: 10000,
|
||
|
referrer: 'https://wechat.com',
|
||
|
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
|
||
|
});
|
||
|
|
||
|
//等待所有内容加载完成
|
||
|
const tab = await hero.activeTab;
|
||
|
await tab.waitForLoad('AllContentLoaded', {timeoutMs: 5000});
|
||
|
|
||
|
const elems = await hero.detach( hero.document.querySelectorAll('meta') );
|
||
|
let meta_name = '';
|
||
|
for (const elem of elems) {
|
||
|
meta_name = elem.getAttribute('name');
|
||
|
if (!meta_name) {continue;}
|
||
|
meta_name = meta_name.toLowerCase();
|
||
|
if (meta_name.indexOf('video_cover_image_url') > -1) {
|
||
|
data.cover = elem.getAttribute('content');
|
||
|
}else if (meta_name.indexOf('video_title') > -1) {
|
||
|
data.title = elem.getAttribute('content');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
await hero.close();
|
||
|
}catch(error) {
|
||
|
console.error('ERROR when request url via hero', url, error);
|
||
|
};
|
||
|
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
export default Douyin;
|