python获取所有qq好友、全部群所有成员部分信息,并保存列表至电子表格文件

20200521更新详细步骤说明

发现上半年压了一堆实用代码,想了一下,还是开源吧。这个就先发布了。

警告
请合理使用此代码,严禁用于广告、信息售卖等反人类用途
使用时请注意不要超负荷访问腾讯服务器

最新代码已传至G站Github:https://github.com/cjh0613/getQQmembers

(欢迎各位改良并Pull Request)

主要使用python+selenium+chrome,加了Tkinter的简单GUI,代码比较简单,就不细说了,请自行品味:

成品:http://d0.ananas.chaoxing.com/download/7694e31d48eedb7b57e5aeefadf67134?fn=qq备份

获取qq好友列表

这个是从QQ充值获取的,可得"原始数据",“分组”,“显示名”,“QQ号”:

  1. 会自动打开QQ充值,请网页登陆
  2. Q币充值,打开充值界面
  3. 充值账号,点更换,打开列表(必须确保此列表打开,再执行下一步)
  4. 点击窗口的’已登陆并打开充值界面,且点开列表’按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import tkinter as tk
from tkinter.filedialog import asksaveasfilename
from bs4 import BeautifulSoup
import lxml
import openpyxl
from openpyxl import Workbook

def callback():
driver.switch_to_frame('webpay-iframe')
iframe =driver.find_element_by_xpath('//*[@id="midas-webpay-main-1450000186"]/div[2]/div[1]/iframe')
driver.switch_to_frame(iframe)
html=driver.page_source
soup=BeautifulSoup(html,"lxml")
a=soup.find_all(attrs={'class':'icon-friend-s'})
wb = Workbook()
ws = wb.active
ws.append(["原始数据","分组","显示名","QQ号"])
for i in a:
if i.next_sibling !=' {{el.name}}({{el.qq}})':
#re,qq匹配:
#pattern = re.compile(r'[1-9][0-9]{4,}')
#re,括号匹配:
#pattern = re.compile(r'(?<=\().*?(?=\))')
#m = pattern.search(i.next_sibling)
k=0
for x in i.next_sibling:

if x == '(':
f=k
if x == ')':
l=k
k=k+1
ws.append([i.next_sibling,i.next_sibling.parent.parent.parent.parent.find(attrs={'class':'icon-more-friend'}).next_sibling,i.next_sibling[:f],i.next_sibling[f+1:l]])
print([i.next_sibling,i.next_sibling.parent.parent.parent.parent.find(attrs={'class':'icon-more-friend'}).next_sibling,i.next_sibling[:f],i.next_sibling[f+1:l]])

wb.save(asksaveasfilename(defaultextension ='.xlsx',filetypes = [('Excel 工作簿', '*.xlsx')]))


#浏览器位置
driver=webdriver.Chrome()

browser =driver
browser.get("https://pay.qq.com/index.shtml")
root = tk.Tk()
# 设置窗口标题
root.title('从QQ充值获取好友列表——峡州仙士制作')
# 设置窗口大小
root.geometry('400x200')
# 进入消息循环(检测到事件,就刷新组件)
button = tk.Button(root, text='已登陆并打开充值界面,且点开列表', command=callback)
button.pack()
root.mainloop()

获取qq好友列表

这个是从QQ群管理获取的,用了pandas库,导出表格超快,可得成员、群昵称、QQ号、性别、Q龄、入群时间、等级(积分)、最后发言等信息:

  1. 选择导出目录,然后会自动打开QQ群管理,请网页登陆
  2. 登陆成功会在选择QQ群界面(必须确保在选择 QQ 群界面,再执行下一步)
  3. 点击窗口的’已登陆并打开界面’按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import tkinter as tk
from tkinter.filedialog import askdirectory
from lxml import etree
import lxml
from bs4 import BeautifulSoup
import pandas as pd
import time

path=askdirectory()

#去字符串两端'\n'、'\t'
def delNT(s):
while s.startswith('\n') or s.startswith('\t'):

s=s[1:]
while s.endswith('\t') or s.endswith('\n'):

s=s[:-1]
return s



def callback():
a=driver.find_elements_by_class_name('icon-def-gicon')
Num= len(a)
time_start=time.time()
for i in range(0,Num):

#点击进入具体群
a=driver.find_elements_by_class_name('icon-def-gicon')
#time.sleep(0.5)
a[i].click()
time.sleep(1)
html=driver.page_source
soup=BeautifulSoup(html,"lxml")
groupTit=delNT(soup.find(attrs={'id':'groupTit'}).text)
groupMemberNum=delNT(soup.find(attrs={'id':'groupMemberNum'}).text)

while len(soup.find_all(attrs={'class':'td-no'}))<int(groupMemberNum):
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
time.sleep(0.1)
html=driver.page_source
soup=BeautifulSoup(html,"lxml")

res_elements = etree.HTML(html)
table = res_elements.xpath('//*[@id="groupMember"]')
table = etree.tostring(table[0], encoding='utf-8').decode()
df = pd.read_html(table, encoding='utf-8', header=0)[0]
try:
print(str(int((time.time()-time_start)/60))+':'+str(int((time.time()-time_start)%60)),'第'+str(i+1)+'群,'+str(int((i+1) / Num * 100))+'% '+groupTit+' 此表完成')
writer = pd.ExcelWriter(path+'/'+groupTit+'.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()
except:
k=0
for v in groupTit:

if v == '(':
f=k
if v == ')':
l=k
k=k+1

writer = pd.ExcelWriter(path+'/'+groupTit[f+1:l]+'.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()
driver.find_element_by_id('changeGroup').click()
time.sleep(1)



driver=webdriver.Chrome()

browser =driver
browser.get("https://qun.qq.com/member.html")
root = tk.Tk()
# 设置窗口标题
root.title('从QQ群管理获取群成员列表——峡州仙士制作')
# 设置窗口大小
root.geometry('400x200')
# 进入消息循环(检测到事件,就刷新组件)
button = tk.Button(root, text='已登陆并打开界面', command=callback)
button.pack()
root.mainloop()

(您还可以在归档页搜索文章标题)