也许我所失去的,正是我生命中绝不该放手的东西。---《云之彼端,约定的地方》
拿代码直接登陆QQ空间有点复杂~说实话我不会~所以就拿cookie
来登陆,cookie
这东西很重要,他包含用户的重要信息~
因为要用cookie登陆,咱们就先去看看cookie
~
打开QQ空间登陆页面,然后打开开发者工具
~接着打开Network
进行捕捉~
登陆上之后,一般捕捉到的第一个里面就包含着登陆的cookie
如下图

红框框里的就是我账号的登陆cookie
咱们开头也说了,这东西很重要,你原封不动的拿过去,服务器也是承认你已经登陆了,对于登陆用户,能干的事情就多了…..
接着,保持开发者工具
不动,打开留言板
找到以get_msgb
开头的~如下图~

点开他,观察一下~

红框框里面的就是咱们要的东西~他在data
下的commentList
里面,接着点开commentList

nickname
就是给你留言的名字
pubtime
是留言时间
uin
是对方的QQ号
htmlContent
是留言的内容
ubbContent
也是留言的内容
htmlContent
和ubbContent
同样是留言内容,不过通过变量名就能猜出了~htmlContent
应该是处理过的ubbContent
是原始的,要选哪一个呐?
咱们选ubbContent
,为啥?仔细看看就知道了~~
OK~来查看一下Headers
里面的链接~

复制这个链接用浏览器打开看一下~

和咱们在开发者工具
里面内容一样
接下来就派出蜘蛛吧~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import requests import json import numpy import time
cookie = "welcomeflash=1473018671_15; ..................................." header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36', 'Connection': 'keep-alive', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cookie': cookie} proxies = {'http':'http://27.185.194.55:8118'} url = "https://h5.qzone.qq.com/proxy/domain/m.qzone.qq.com/cgi-bin/new/get_msgb?uin=1473018671&hostUin=1473018671&start=0&s=0.6099869503510162&format=jsonp&num=10&inCharset=utf-8&outCharset=utf-8&g_tk=1911197127&qzonetoken=40006667030cbf36e52d4a80e349d674e7024da4780b92a66c3f5f3b9e413903b691aac1b0d294bfbe" res = requests.get(url,proxies=proxies,headers=header) print(res.text)
|
代码里面我的cookie
我去掉了~url
地址也是拿自己的~从开发者工具
上复制就行了~
运行一下:

没问题~和刚才打开的内容一样~不过现在弄下来的是一个字符串~不能直接json
先用切片把_Callback()
去掉~
1 2 3 4 5 6 7 8 9 10 11 12 13
| cookie = "welcomeflash=1473018671_15; ............................" header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36', 'Connection': 'keep-alive', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cookie': cookie} proxies = {'http':'http://27.185.194.55:8118'} url = "https://h5.qzone.qq.com/proxy/domain/m.qzone.qq.com/cgi-bin/new/get_msgb?uin=1473018671&hostUin=1473018671&start=0&s=0.6099869503510162&format=jsonp&num=10&inCharset=utf-8&outCharset=utf-8&g_tk=1911197127&qzonetoken=40006667030cbf36e52d4a80e349d674e7024da4780b92a66c3f5f3b9e413903b691aac1b0d294bfbe" res = requests.get(url,proxies=proxies,headers=header) get_text = res.text[10:-3] js = json.loads(get_text) for i in range(len(js['data']['commentList'])): print(js['data']['commentList'][i]['nickname'],'\t',js['data']['commentList'][i]['ubbContent'])
|

成啦~这一页没问题~下一页咋办呐?~
继续观察开发者工具
点击下一页~

看一下这个链接和上一个的链接有啥变化~~
在这个链接里面,咱们就只需要修改两个地方~一个是start
另一个是s
的参数
start
是0、10、20、30、40、50、60……这样一直上去的,用个while
循环进行了
s
是随机的时间,可以用random.random
或者numpy.random.random
实现,两个都一样,我就用第二个吧~
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
| import requests import json import numpy import time
cookie = "" # 输入自己的 cookie header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36', 'Connection': 'keep-alive', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cookie': cookie} proxies = {'http':'http://27.185.194.55:8118'} times = 0 uin = input("QQ帐号: ") print("请注意更改url中qzonetoken和g_tk参数") if cookie == "": print("请输入cookie!") else: while True: print("第 {} 页".format(((times//10)+1))) print("----------------------------------------") url = "https://h5.qzone.qq.com/proxy/domain/m.qzone.qq.com/cgi-bin/new/get_msgb?uin={}&hostUin={}&num=10&start={}&hostword=0&essence=1&s={}&iNotice=0&inCharset=utf-8&outCharset=utf-8&format=jsonp&ref=qzone&g_tk=159637896&qzonetoken=2a26403b960220332f267c6736eec1e4053e1f6eb5791eb2fe314429eb084741d2f4b70466ac0e7f1c".format(uin,uin,str(times),numpy.random.random()) # url地址要自己去开发者工具里面拿~上面的这个是我自己的~复制自己的修改一下就行了~ regs = requests.get(url, proxies=proxies, headers=header) str_qiepian = regs.text[10:-3] try: js = json.loads(str_qiepian)['data']['commentList'] except KeyError: print("请更换cookie!") break except json.decoder.JSONDecodeError: print("请更换url地址,地址中的qzonetoken验证和g_tk有变") break if js == []: print("Over! 共计 {} 页".format(json.loads(str_qiepian)['data']['total'])) break else: for page in range(len(js)): print("第{}页的第{}条 留言者:{} \t 内容:{}".format((times//10)+1,page+1,js[page]['nickname'],js[page]['ubbContent'])) times+=10 time.sleep(numpy.random.random())
|
注意啊!修改自己的cookie和url
url只需要更改qzonetoken和g_tk参数
效果:

就到这儿吧~有问题请在下面评论区留言~~各位大佬手下留情~
源代码:爬取QQ空间留言板