作者skyscraper23 (任我行)
看板java
标题[语法] 想请问各位大神这个code是不是有问题
时间Sat Nov 7 20:52:56 2020
各位好,前几天用下列程式码爬了这个粉专2017-2019的资料,但电脑一直转不确定是什麽因素,可以请各位大神帮我看看是不是有什麽问题,感激不尽!
https://www.facebook.com/equallovetw/
—————————————————————
var auto_slide = true;
let data = [];
function load_list()
{
if (!auto_slide) {
return
}
window.scroll(0, window.pageYOffset + 500);
showDetailArticle();
removeExtraArticle();
setTimeout(function () {
load_list();
}, 350);
}
function removeExtraArticle()
{
var rows = document.querySelectorAll(':not(li) > div[class="_4-u2 _4-u8"]');
if (rows.length < 40) {
return;
}
for (let s=0; s<= rows.length; s++) {
if (s >= 20) {
break;
}
if (rows[s] && rows[s].querySelector('abbr[data-utime]')) {
if (rows[s].querySelector('abbr[data-utime]').getAttribute('title').contains('2020')) {
rows[s].remove();
}
}
}
}
function showDetailArticle()
{
var rows = document.querySelectorAll(':not(li) > div[class="_4-u2 _4-u8"]');
for (let s=0; s<= rows.length; s++) {
if (rows[s] && rows[s].querySelector('abbr[data-utime]')) {
if (rows[s].querySelector('abbr[data-utime]').getAttribute('title').contains('2016')) {
auto_slide = false;
return;
}
if (
rows[s].querySelector('abbr[data-utime]').getAttribute('title').contains('2019')
|| rows[s].querySelector('abbr[data-utime]').getAttribute('title').contains('2018')
|| rows[s].querySelector('abbr[data-utime]').getAttribute('title').contains('2017')
) {
if (rows[s].querySelector('.see_more_link_inner')) {
rows[s].querySelector('.see_more_link_inner').click();
}
setTimeout(function () {
this.target.classList.add('more-clicked');
}.bind({
target: rows[s],
}), 500);
rows[s].classList.add('more-clicking');
}
}
}
}
function download(data) {
const filename = 'output.tsv';
const blob = new Blob([data], {type: 'text/json'});
const e = document.createEvent('MouseEvents');
const a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
}
function captureArticle()
{
console.log('zzzzzzz');
var rows = document.querySelectorAll(':not(li) > div._4-u2._4-u8.more-clicked');
for (let s=0; s< rows.length; s++) {
if (rows[s] && rows[s].querySelector('[data-testid="post_message"]') && rows[s].querySelector('abbr[data-utime]')) {
// console.log('----------------------------------------------------------------');
// console.log('----------------------------------------------------------------');
// console.log(rows[s].querySelector('abbr[data-utime]').getAttribute('title'), rows[s].querySelector('[data-testid="post_message"]').innerText);
/* data.push({
date: rows[s].querySelector('abbr[data-utime]').getAttribute('title'),
text: rows[s].querySelector('[data-testid="post_message"]').innerText,
liked: rows[s].querySelector('span._81hb').textContent,
shared: rows[s].querySelector("a[class='_3rwx _42ft']").text.split(' ')[0],
}); */
let liked = rows[s].querySelector('span._81hb') || '0';
if (liked !== '0') {
liked = liked.textContent;
}
let shared = rows[s].querySelector("a[class='_3rwx _42ft']") || '0';
if (shared !== '0') {
shared = shared.text.split(' ')[0]; // a._3rwx _42ft <a>text</a>
}
data.push([
rows[s].querySelector('abbr[data-utime]').getAttribute('title'),
rows[s].querySelector('[data-testid="post_message"]').innerText,
liked,
shared,
]);
// console.log(data[data.length-1]);
}
rows[s].remove();
}
if (!auto_slide && rows.length === 0) {
auto_slide = false;
console.log("Scrape End");
let str = 'Date\tText\tLiked\tShared';
for (const post in data) {
str += ('\n'+data[post].map(d => `"${d.replaceAll('"', '""')}"`).join('\t'));
}
console.log(str);
download(str);
return;
}
setTimeout(function () {
captureArticle();
}, 5000);
}
load_list();
captureArticle();
-----
Sent from JPTT on my iPhone
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 101.10.4.19 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1604753578.A.35E.html
1F:→ fgh81113: 狗和热狗的问题吧 11/07 22:26
2F:→ qrtt1: 这问题大惹 11/08 09:36
3F:→ skyscraper23: 可以请问一下是什麽问题嘛? 11/08 11:41
4F:→ skyscraper23: 因为我抓另外一个粉专是可以的,但这一个抓不太下来 11/08 11:41
5F:推 iceman5566: 这是JavaScript 然後你来Java版问 看出问题了吧 11/08 12:27
6F:→ adsl12367: 楼上XD 11/09 17:26
7F:→ jej: 这让我想到这版好像还没有人问过Rhinos的样子 11/11 12:58
8F:→ yoche2000: 恩 狗和热狗 满中肯的 11/11 14:16
9F:→ Cavalier: XD 11/12 09:29
10F:推 bitlife: 狗也是可以是一种热狗,不管是直接加热或者料理後加热(误) 11/12 14:46
11F:推 kof2200: 然後楼上会被动保人士关切。 11/12 19:44
12F:推 James610024: 看到let还往上滑,以为走错版 11/17 21:04