Python分析好友空间星座代码

通过学习Python的使用,可以在空间获取好友星座分布图

代码如下

#1、导入模块

import json
import requests
from lxml import etree
import pandas as pd
import pygal
import pickle

#2、 获取qq好友里全部星座信息 

#获得字典形式的cookies,每次用都要更新

cookiestext = '自己QQ空间的cookies'

def dictcookies(cookiestext): # 把字符串格式的cookies转化为字典格式
    cookies = {} # 目标字典
    cookiespure = cookiestext.replace(' ','')
    cookieslist = cookiespure.split(';')
    for i in cookieslist:
        key,value = i.split('=',1)
        cookies[key] = value
    # print(cookies) # 需要的话可以打印一下观察结果
    return cookies
def savetofile(filename,content):
    save_file = open(filename,'wb')
    pickle.dump(content,save_file)

def readfile(filename):
    load_file = open(filename,'rb')
    data_file = pickle.load(load_file)
    return data_file

cookies = dictcookies(cookiestext)

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'}

# 获得好友空间的url地址列表,先要获得储存好友信息的json文件
frdjsurl = 'https://user.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_ship_manager.cgi?uin=491034714&do=1&rd=0.7684480696337077&fupdate=1&clean=1&g_tk=1578640073&qzonetoken=6b459c18d1ed1f360ddf1f19cf3b2ec8ffa91df433aea503381f8ac7d67f312178b2304d498f41d6c8f3&g_tk=1578640073'
frdjspage = requests.get(frdjsurl, headers=headers, cookies=cookies)

# 解析json文件
temp = json.loads(frdjspage.text[10:-2])
a = temp['data']['items_list']
# 生成存储好友昵称和qq号的字典
friends = {}
for i in a: 
    friends[i['uin']] = {'name':i['name']}
for k in friends.keys():
    friends[k]['link'] = 'https://user.qzone.qq.com/'+str(k)

3、#获取星座相关信息
for v in friends.values():
    frdpage = requests.get(v['link'], headers=headers, cookies=cookies)
    a = etree.HTML(frdpage.text)
    title = a.xpath('/html/head/title')[0].text
    if title != 'QQ空间-分享生活,留住感动':
        print(str(title) + '的空间打得开')
        v['open'] = True
        info = a.xpath('//*[@id="profile-detail"]/div/div[2]/ul/li[1]/div[2]/h4')
        info1 = a.xpath('//*[@id="profile-detail"]/div/div[2]/ul/li[2]/div[2]/h4')
        if info:
            if info[0].text:
                if info[0].text.find('座') != -1:
                    itemp = info[0].text.find('座')
                    xingzuo = info[0].text[itemp-2:itemp+1]
                    print(xingzuo)
                    v['xingzuo'] = xingzuo
                elif info1:
                    if info1[0].text:
                        if info1[0].text.find('座') != -1:
                            itemp = info1[0].text.find('座')
                            xingzuo = info1[0].text[itemp-2:itemp+1]
                            print(xingzuo)
                            v['xingzuo'] = xingzuo

4、#做成表格,方便可视化处理  

''' 
#分别使用 isin( )和~isin( )删选数据
starlist = ['白羊座','金牛座','双子座', '巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座','水瓶座','双鱼座']
xz = fr['xingzuo'][fr['xingzuo'].isin(starlist)]
xzc = xz.value_counts()

starlist = ['无法打开主页获取','没有相关信息']
xz = fr['xingzuo'][~fr['xingzuo'].isin(starlist)]
xzc = xz.value_counts()
'''

fr = pd.DataFrame(friends).transpose()# 设置一下转置
starlist = ['白羊座','金牛座','双子座', '巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','魔羯座','水瓶座','双鱼座']
xz = fr['xingzuo'][fr['xingzuo'].isin(starlist)]
xzc = xz.value_counts()
print(xzc)
# 可视化 横柱图
chart = pygal.HorizontalBar()
chart.title = '好友星座的分布图'
for i in range(len(xzc)):
    chart.add(xzc.index[i],xzc.iloc[i],rounded_bars=5)#可以修改数字5,数字越大,柱状图两头越圆润  
chart.render_to_file('xz.svg')

你可能感兴趣的:(Python分析好友空间星座代码)