#### 订阅号好像没有获取用户信息的接口,所以就自己写了个脚本进行获取
package com.jeffrey.manager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.*;
public class GetUserDataId {
private static final Map USER_DATA_MAP = new HashMap<>();
private static final String CHROMEDRIVER_DRIVER = "src/com/jeffrey/utils/chromedriver";
private static final int SPEED = 2000;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", CHROMEDRIVER_DRIVER);
WebDriver driver = new ChromeDriver();
driver.get("https://mp.weixin.qq.com/");
WebDriverWait wait = new WebDriverWait(driver, 100);
System.out.println("等待扫码登录");
wait.until(new ExpectedCondition() {
@Override
public WebElement apply(WebDriver webDriver) {
return driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div[2]/div[2]/ul[1]/li[3]/em/a"));
}
}).click();
int allUserCount = Integer.parseInt(driver.findElement(By.cssSelector("#groupsList > dl:nth-child(1) > dt > a > em")).getText().replace("(", "").replace(")", ""));
while (true) {
try {
Thread.sleep(SPEED);
} catch (InterruptedException e) {
e.printStackTrace();
}
String[] pn;
try {
List idEle = driver.findElements(By.className("js_msgSenderAvatar"));
List nameEle = driver.findElements(By.className("remark_name"));
for (int i = 0; i < idEle.size(); i++) {
USER_DATA_MAP.put(idEle.get(i).getAttribute("data-id"), nameEle.get(i).getText());
}
pn = wait.until(new ExpectedCondition() {
@Override
public WebElement apply(WebDriver webDriver) {
return driver.findElement(By.className("page_num"));
}
}).getText().replace(" ", "").split("/");
} catch (RuntimeException e) {
e.printStackTrace();
continue;
} finally {
System.out.println("已获取 " + USER_DATA_MAP.size() + " 个用户信息,共 " + allUserCount + " 位用户");
}
if (Integer.parseInt(pn[0]) == Integer.parseInt(pn[1])) {
if (allUserCount == USER_DATA_MAP.size()) {
System.out.println("用户总数" + USER_DATA_MAP.size());
Set> entries = USER_DATA_MAP.entrySet();
for (Map.Entry bean : entries) {
System.out.println("用户 id:" + bean.getKey() + "\n用户昵称:" + bean.getValue() + "\n----------\n");
}
driver.close();
break;
} else {
throw new RuntimeException("获取到的用户信息条目数与总条目数不符");
}
} else {
wait.until(new ExpectedCondition() {
@Override
public WebElement apply(WebDriver webDriver) {
return driver.findElement(By.xpath("/html/body/div[2]/div/div[3]/div/div/div[4]/div[2]/div[1]/div/div[3]/div/div/span[1]/a[3]"));
}
}).click();
}
}
}
}