Skip to content

Instantly share code, notes, and snippets.

@bearlyrunning
Last active July 13, 2018 02:08
Show Gist options
  • Save bearlyrunning/748717f87fe495346d40f7f3fdca5794 to your computer and use it in GitHub Desktop.
Save bearlyrunning/748717f87fe495346d40f7f3fdca5794 to your computer and use it in GitHub Desktop.
Zhihu cleanup script - unfollow
// This script saves all followed question urls and titles in a hashmap => data.json
// { "url":"title", "url":"title", ...}
const puppeteer = require('puppeteer');
var fs = require('fs');
(async () => {
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage()
const pagenum = 25 // This is the number of pages + 1
// const screenshot = 'blah.png';
var data = {}
await page.goto('https://www.zhihu.com/')
await page.setViewport({
width: 1280,
height: 600
}) // Make screen bigger so that loginButton is visible
let loginButton = `button.Button.HomeSidebar-signBannerButton.Button--blue.Button--spread`
await page.click(loginButton)
// This one is a bit flaky, but ceebs with tuning, TBC
page
.waitForSelector('[name="username"]')
.then()
await page.type('[name="username"]', process.env.ZHIHU_USER)
await page.type('[name="password"]', process.env.ZHIHU_PASS)
// Click it manually if Zhihu prompts user-computer check
const signinButton = `button.Button.SignFlow-submitButton.Button--primary.Button--blue`
await page.click(signinButton)
await page.waitForNavigation()
for (var i = 1; i < pagenum; i++) {
await page.goto('https://www.zhihu.com/people/username/following/questions?page=' + i)
let pg = await page.evaluate((data) => {
let dt = {}
let temp = document.getElementsByClassName("QuestionItem-title")
for (var t of temp)
dt[t.children[0].href] = t.children[0].innerHTML
return dt
})
data = Object.assign(pg,data)
}
fs.writeFile('./data.json', JSON.stringify(data), 'utf-8', (err) => {
if (err) throw err;
console.log('The file has been saved!')
})
// await page.screenshot({ path: screenshot })
await browser.close()
})()
// Created this script to automatically unfollow questions on Zhihu
// As all the following activities are displayed to public and that's bad for privacy.
const puppeteer = require('puppeteer');
var fs = require('fs');
(async () => {
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage()
// List of button selectors
const loginButton = `button.Button.HomeSidebar-signBannerButton.Button--blue.Button--spread`
const unfollowButton = `button.Button.FollowButton.Button--primary.Button--grey`
// data.json contains { url:question, url:question, ...}, this is pre-generated from questions.js
var urlObj = JSON.parse(fs.readFileSync('./data.json', 'utf8'))
await page.goto('https://www.zhihu.com/')
await page.setViewport({
width: 1280,
height: 600
})
await page.click(loginButton)
page
.waitForSelector('[name="username"]')
.then()
await page.type('[name="username"]', process.env.ZHIHU_USER)
await page.type('[name="password"]', process.env.ZHIHU_PASS)
// Manually click login button as Zhihu detects automated click
// const signinButton = `button.Button.SignFlow-submitButton.Button--primary.Button--blue`
// await page.click(signinButton)
// await page.waitForNavigation()
await page.waitFor(10000);
var count = 1
for (var u in urlObj) {
console.log(u)
await page.goto(u)
await page.click(unfollowButton)
if (count == 1) {
// Zhihu also pops up for 2FA when it detects automated click for the first time after login
// Wait for a minute to complete 2FA
await page.waitFor(60000);
count = 0
}
}
await browser.close()
})()
@hrdxwandg
Copy link

你好,问下你这个怎么使用呢?

@hrdxwandg
Copy link

运行node questions.js时报以下错误:

(node:81986) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: text is not iterable
(node:81986) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

问下这个怎么解决呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment