南强小屋 Design By 杰米
具体代码如下所示:
#coding:utf-8
#!/usr/bin/python3
from selenium import webdriver
import time
import re
import importlib2
import sys
importlib2.reload(sys)
def startSpider():
driver = webdriver.Chrome('/Users/zachary/zachary/chromedriver.exe') #这个是chormedriver的地址
driver.get('https://qzone.qq.com/')
driver.switch_to.frame('login_frame')
driver.find_element_by_id('switcher_plogin').click()
driver.find_element_by_id('u').clear()
driver.find_element_by_id('u').send_keys('QQ号') #这里填写你的QQ号
driver.find_element_by_id('p').clear()
driver.find_element_by_id('p').send_keys('QQ密码') #这里填写你的QQ密码
driver.find_element_by_id('login_button').click()
time.sleep(2)
#设置爬取内容保存路径
f = open('/Users/zachary/Documents/shuoshuo.txt','w')
#---------------获得g_qzonetoken 和 gtk
html = driver.page_source
'''g_qzonetoken=re.search('window\.g_qzonetoken = \(function\(\)\{ try\{return (.*"e794139a284d6ea9e0b26826e541b55df37d0667a3544f534de25aebdb64628d3ab75e1d7104bbb22a"
cookie = {}#初始化cookie字典
for elem in driver.get_cookies():#取cookies
cookie[elem['name']] = elem['value']
gtk=getGTK(cookie)#通过getGTK函数计算gtk
#print(g_qzonetoken)
#print(gtk)
#--------------获得好友列表 注意下面的链接
driver.get('https://user.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_hat_get.cgi"(.\d*)\":\{\\n"realname":"(.*"}',re.S)
QQ_name_list = re.findall(abtract_pattern,str(friend_list)) #数组
print(QQ_name_list)
numList=dict()# numList => (QQnum:QQname) #列表
for i in QQ_name_list:
numList[str(i[0])]=str(i[1])
begin = 0
last_source = ""
tag = 1
first = 0
firstTime=""
#如果要爬取自己的说说,手动添加自己的qq号
#numList['你的qq号']='你的名字'
#print(numList)
for key in numList.keys():
QQnum = key
QQname = numList[QQnum]
if QQnum == "好友qq号": #根据qq号查找指定好友说说
count = 1
begin = 0
while tag==1 :
#-------------进入好友说说页面 #'+QQnum+' '+str(begin)+'
#print("Begin:"+str(begin))
driver.get('https://user.qzone.qq.com/proxy/domain/taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6"message":"(.*","name":',re.S)
message = re.findall(abtract_pattern,str(msg_list_json))
if message!=[]:
if str(message[0])=='对不起,主人设置了保密,您没有权限查看':#对不起,主人设置了保密,您没有权限查看
break
#print(msg_list_json)
#解析JSON
#webDriver没有现成的JSON解析器,所以采用获取源码的方式,然后使用正则表达式获取具体细节
msg_list_json = msg_list_json.split("msglist")[1]#拆分json,缩小范围,也能加快解析速度
msg_list_json = msg_list_json.split("smoothpolicy")[0]
msg_list_json = msg_list_json.split("commentlist")[1:]
#说说动态分4种:1、文字说说(或带有配图的文字说说)
# 2、只有图片的说说
# 3、转发,并配有文字
# 4、转发,不配文字
for text in msg_list_json:
# 1、先检查说说,用户是否发送了文字,如果没有文字,正则表达式匹配无效
abtract_pattern = re.compile('\}\],"content":"(.*","createTime":"(.*","created_time":(.*"',re.S)
msg_time = re.findall(abtract_pattern,str(text))
if msg_time!=[]:
# 2、如果作者说说有文字,那么检查是否有转发内容
msg = str(msg_time[0][0])
sendTime = str(msg_time[0][1])
abtract_pattern = re.compile('\}\],"content":"(.*"},"rt_createTime":"(.*","',re.S)
text = text.split("created_time")[1]
msg_time2 = re.findall(abtract_pattern,str(text))
#合并发送内容 格式:评论+转发内容
if msg_time2!=[]:
msg = msg +" 转发内容:"+str(msg_time2[0][0])
else:
# 3、说说内容为空,检查是否为 =>只有图片的说说 or 转发,不配文字
#获取正文发送时间 (发送时间分为:正文发送时间 or 转发时间)
abtract_pattern = re.compile('"conlist":null,"content":"","createTime":"(.*",',re.S)
msgNull_time = re.findall(abtract_pattern,str(text))
if msgNull_time!=[]:
#如果有正文发送时间,那么就是这条说说仅含有图片 =>只有图片的说说
msg = "图片"
sendTime = str(msgNull_time[0])
else:
#如果没有正文发送时间,那么就是说这条说为 =>转发,不配文字
abtract_pattern = re.compile('\}\],"content":"(.*"},"rt_createTime":"(.*","',re.S)
msg_time = re.findall(abtract_pattern,str(text))
msg =" 转发内容:"+str(msg_time[0][0])
sendTime = str(msg_time[0][1])
#写入本地文件
#f.write('{},{},{},{}\n'.format(str(QQname),str(QQnum),sendTime,msg))
print(str(count)+" : "+str(QQname)+" : "+str(QQnum)+" : "+sendTime+" : "+msg)
count = count + 1
begin = begin + 40
def getGTK(cookie):
hashes = 5381
for letter in cookie['p_skey']:
hashes += (hashes << 5) + ord(letter)
return hashes & 0x7fffffff
startSpider()
print("爬取结束")
总结
以上所述是小编给大家介绍的Python爬取qq空间说说的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米
暂无Python爬取qq空间说说的实例代码的评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。