下面这段代码的逻辑起点是页面滑到底部ajax加载的一个链接url_initial
(我是在手机微信上在浏览某个公众号的某个页面时send to Chat
到自己账号,获得链接后在电脑chrome上打开; 或者可以通过关注 “My Evernote”,把链接发到Evernote的笔记里; 或者在ios平台上好像有发送到邮件的功能,也可以得到这个链接; 但是直接在浏览器中打开或者之间拷贝链接是无效的。然后在浏览器中打开链接,通过检测浏览器访问的Network
->XHR
找到需要的链接的)
通过观察发现这个加载功能的接口每次访问时返回10个条目,而访问时传到服务器的参数只有frommsgid
是改变的,而且是上一次访问的最后一个条目的id
。所以可以递归获得所有的条目。
这个加载接口的参数还有几个是和session相关的,导致只能存活20min左右,这时候需要重新通过手机微信去获得
url_initial
访问接口的频率不能太快,一次循环结束需要加一句
time.sleep(t)
其中t>0.5
时应该是安全的,否则会被临时禁止访问单单通过这个方法获得不到阅读数和评论数,http://tool.qoofan.com/weixin/query这个网站提供了API查询阅读数和评论数(但是收费),当然也可以自己尝试着去寻找漏洞自己写API
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from pyquery import PyQuery as pq
import requests
import codecs
import json
import time
import datetime
def ttd(timestamp):
timeStr=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
return timeStr
f=codecs.open('/home/allen/projects/sjtu_news/data_wechat.csv','w','utf-8')
url_initial='https://mp.weixin.qq.com/mp/getmasssendmsg?__biz=MjM5MDIyMDQyMA==&uin=MjMzNTA3MjQw&key=8dcebf9e179c9f3a15be50268fa7072ea98eab64f47c243ed0a75d48addee99ee76f6585fb67fd0f15a6d32009bfe38b&f=json&frommsgid=1000000038&count=10&uin=MjMzNTA3MjQw&key=8dcebf9e179c9f3a15be50268fa7072ea98eab64f47c243ed0a75d48addee99ee76f6585fb67fd0f15a6d32009bfe38b&pass_ticket=OHlwEqWexZOnMR8LoFoVhpLM7RXg28HZhMrsoP4Rrc0%25253D&wxtoken=&x5=0'
url_head=url_initial[0:url_initial.index('frommsgid=')+len('frommsgid=')]
url_tail=url_initial[url_initial.index('&count='):]
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/600.5.17 (KHTML, like Gecko) Version/8.0.5 Safari/600.5.17"}
def run(url_initial):
page=requests.get(url_initial)
page.encoding='utf-8'
page_json=json.loads(page.text)
general_msg_list=page_json['general_msg_list']
general_msg_list=json.loads(general_msg_list)
lists=general_msg_list['list']
# print lists[0]['app_msg_ext_info']['multi_app_msg_item_list'][0]['title']
last_id=general_msg_list['list'][9]['comm_msg_info']['id']
# main_title=[general_msg_list['list'][i]['app_msg_ext_info']['title'] for i in range(10)]
main_title=[]
for i in range(10):
try:
main_title.append(general_msg_list['list'][i]['app_msg_ext_info']['title'])
except:
main_title.append(general_msg_list['list'][i]['comm_msg_info']['content'])
datetime=[general_msg_list['list'][i]['comm_msg_info']['datetime'] for i in range(10)]
for i in range(10):
try:
multi_msg=[general_msg_list['list'][i]['app_msg_ext_info']['multi_app_msg_item_list'] for i in range(10)]
except:
multi_msg=[]
i=0
for j in multi_msg:
f.write(str(ttd(datetime[i]))+'\t')
f.write(main_title[i]+'\t')
i=i+1
for k in j:
f.write(k['title']+'\t')
f.write('\n')
return last_id
for i in range(1000):
rv=run(url_initial)
print rv
time.sleep(2)
url_initial=url_head+str(rv)+url_tail
# print url_initial