爬虫(三)生成qq好友关系网(1)—登录并获得好友列表

本文目标

  1.使用selenium实现账号密码登录qq空间

  2.使用多线程爬取qq好友的说说评论点赞情况保存至本地

  3.使用echarts将数据可视化


开源地址:qq好友关系爬虫

这是帮我同学做的他的qq好友关系网效果图,觉得他的关系网络比较好看,有特点,所以就用他的了,连线代表两人之间有联系,线的粗细代表两人关系的好的程度,为了显示效果和隐私就把名字给去了

 

最开始是看到了这篇博客,https://blog.csdn.net/u014271114/article/details/54973598,觉得得到好友关系网好像很有意思,无奈原博主并没有给出源码,自己瞎鼓捣了下,觉得有点思路,于是就开始做啦

 

 一. 使用selenium登录获取好友列表

  •   selenium真的是非常好用,仿佛打开新世界大门,是模仿用户操作的一个自动化测试工具,想到之前模拟登录还要手动复制cookie真的是捞,关于selenium的安装配置看这篇文章:win10下配置python+selenium,看谷歌浏览器的版本号和对应的driver看这个: 版本号

  1. 实现账号密码登录

      注意要切换到登录的框架,代码如下

from selenium import webdriver
from time import sleep
import json

#chromedriver = 'C:/Users/13180/AppData/Local/Google/Chrome/Application/chromedriver.exe'
#driver = webdriver.Chrome(chromedriver)
driver = webdriver.Chrome()
driver.get('https://user.qzone.qq.com/你的qq/main')
#切换到登录的框架
driver.switch_to.frame('login_frame')
#找到账号密码登陆的地方
driver.find_element_by_id('switcher_plogin').click()
driver.find_element_by_id('u').send_keys('你的qq')
driver.find_element_by_id('p').send_keys('你的密码')
driver.find_element_by_id('login_button').click()
#保存本地的cookie
sleep(3)
cookies = driver.get_cookies()
cookie_dic = {}
for cookie in cookies:
    if 'name' in cookie and 'value' in cookie:
        cookie_dic[cookie['name']] = cookie['value']
    with open('cookie_dict.txt', 'w') as f:
        json.dump(cookie_dic, f)

将这个保存为cookie.py,运行起来就能看到自动打开浏览器,然后会在目录下生成cookie_dict.txt的文件啦,接下来的访问都带上这个cookie就可以保持登录状态了


2. 获取qq好友列表

  •  是通过这里得到qq好友列表,这种方法只能得到亲密度前200的好友

  •  打开F12进入开发者中心,点击  好友  ,看到

   发送的http请求到的url为 https://user.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_ship_manager.cgi

带的参数数据如下

返回的数据是类似json格式的,只不过多了头_Callback(),只需要简单的正则匹配一下把头给去了就行啦

 

所以现在就根据前面得到的信息得到好友的qq,和备注,代码如下

 #找出好友qq和备注列表
    def get_friend(self):
        url_friend = 'https://user.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_ship_manager.cgi?'
        g_tk = self.get_gtk()
        uin= self.get_uin()
        data = {
            'uin': uin,
            'do' : 1,
            'g_tk': g_tk
        }
        data_encode = urllib.parse.urlencode(data)
        url_friend += data_encode
        res = requests.get(url_friend, headers = header, cookies = cookie)
        #正则匹配出_Callback()里面的内容
        friend_json = re.findall('\((.*)\)',res.text,re.S)[0]
        #变成字典类型
        friend_dict = json.loads(friend_json)
        friend_result_list = []
        #循环将好友的姓名qq号存入list中
        for friend in friend_dict['data']['items_list']:
            friend_result_list.append([friend['name'],friend['uin']])
        #得到的好友list是[[name1,qqNum1],[name2,qqNum2],.....]格式的
        return friend_result_list

 

然后构造一个Qzone爬虫类,计算gtk,获得好友列表,

import urllib
import requests
import json
import re

class Qzone:

    #算出来gtk,这个算法百度一下就行啦,不要纠结于这个
    def get_gtk(self):
        p_skey = cookie['p_skey']
        h = 5381
        for i in p_skey:
            h += (h << 5) + ord(i)
            g_tk = h & 2147483647
        return g_tk

    #得到uin
    def get_uin(self):
        uin = cookie['ptui_loginuin']
        return uin

    #只得到好友qqlist,以便后面判断共同好友
    def get_qq(self):
        qq_list = []
        friend_list = self.get_friend()
        for friend in friend_list:
            qq_list.append(friend[1])
        return qq_list


    #找出好友qq和备注列表
    def get_friend(self):
        url_friend = 'https://user.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_ship_manager.cgi?'
        g_tk = self.get_gtk()
        uin= self.get_uin()
        data = {
            'uin': uin,
            'do' : 1,
            'g_tk': g_tk
        }
        data_encode = urllib.parse.urlencode(data)
        url_friend += data_encode
        res = requests.get(url_friend, headers = header, cookies = cookie)
        #正则匹配出_Callback()里面的内容
        friend_json = re.findall('\((.*)\)',res.text,re.S)[0]
        #变成字典类型
        friend_dict = json.loads(friend_json)
        friend_result_list = []
        #循环将好友的姓名qq号存入list中
        for friend in friend_dict['data']['items_list']:
            friend_result_list.append([friend['name'],friend['uin']])
        #得到的好友list是[[name1,qqNum1],[name2,qqNum2],.....]格式的
        return friend_result_list

if __name__ == '__main__':
    qzone = Qzone()
    #假装我是浏览器
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:61.0) Gecko/20100101 Firefox/61.0",
        "Accepted-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    }
    #读出cookie的内容
    with open('cookie_dict.txt','r') as f:
        cookie = json.load(f)

    #得到qq列表,
    qq_list = qzone.get_qq()

 

现在已经得到了好友列表,接下来该访问每个好友的空间,爬取说说的点赞和评论情况

  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要使用Java编写一个爬取Windows微信好友列表爬虫,你可以使用Selenium WebDriver来自动化控制浏览器进行操作。以下是一个示例: 首先,确保已经安装了Java和Selenium WebDriver,并下载了适用于你所使用的浏览器的WebDriver驱动程序。 ```java import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class WeChatFriendListCrawler { public static void main(String[] args) { // 设置WebDriver驱动程序的路径 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // 实例化WebDriver WebDriver driver = new ChromeDriver(); // 打开微信网页版 driver.get("https://wx.qq.com/"); // 等待用户扫描二维码登录 // 这里需要手动扫描二维码登录微信 // 等待页面加载完成 // 这里可以根据实际情况调整等待时间,确保页面加载完成 try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // 点击通讯录按钮 WebElement contactsButton = driver.findElement(By.xpath("//div[@title='通讯录']")); contactsButton.click(); // 获取好友列表 WebElement friendList = driver.findElement(By.id("J_NavChatScrollBody")); System.out.println(friendList.getText()); // 关闭浏览器 driver.quit(); } } ``` 以上代码使用Chrome浏览器作为示例,你需要将`"path/to/chromedriver"`替换为你实际的ChromeDriver驱动程序的路径。在运行代码之前,请确保你已经登录了微信网页版,并且已经扫描了二维码进行登录。 这只是一个简单的示例,实际上,爬取微信好友列表可能需要更复杂的操作和处理。另外,请注意爬取网页内容时要遵守相关法律法规和网站的使用条款,并确保你的行为合法合规。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值