微信运动数据抓取(Python)

时间:2018-05-20 18:07:19   收藏:0   阅读:2692

“微信运动”能够向朋友分享一个包含有运动数据的网页,网页中就有我们需要的数据。url类似于:http://hw.weixin.qq.com/steprank/step/personal?openid=用户的openid,其中有用于对于微信运动的唯一openid,打开fiddler进行抓包,首先打开fiddler,然后打开微信运动点击我的主页,如下
技术分享图片
微信通过请求头区分是否是通过微信浏览器进行的请求,如果用浏览器直接打开链接会出现如下错误提示,说明不是通过微信浏览器打开被微信拦截了:
技术分享图片
通过Fiddler的抓包数据我们可以通过伪造Request Headers请求头抓取数据
Fiddler抓包显示:
技术分享图片
通过postman伪造请求头模拟微信浏览器。伪造请求头后成功在浏览器中得到了对应的网页内容:
技术分享图片

Python实现代码:

import requests
import re
import json


class WechatSprot(object):
    def __init__(self, openid):
        self.openid = openid

    def getInfo(self):
        url = "http://hw.weixin.qq.com/steprank/step/personal"

        querystring = {"openid": self.openid}

        headers = {
            ‘host‘: "hw.weixin.qq.com",
            ‘connection‘: "keep-alive",
            ‘accept‘: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            ‘user-agent‘: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.691.400 QQBrowser/9.0.2524.400",
            ‘accept-encoding‘: "gzip, deflate",
            ‘accept-language‘: "zh-CN,zh;q=0.8,en-us;q=0.6,en;q=0.5;q=0.4",
            ‘cookie‘: "hwstepranksk=JxMBWw1sxQhxnMgsJnnLh-r0VFzLH6RtJWv5b_j3z8MPs6-J; pass_ticket=p9R%2FqjIh%2BlXt%2BoxP7GIWrqm3Sbf1Minisk%2FNUz5zra4ReETR2ATI8H57zkEERCvG",
        }

        response = requests.request("GET", url, headers=headers, params=querystring)

        res = re.findall(‘window.json = (.+);‘, response.text)
        # print(res)
        # exit()
        return json.loads(res[0])


if __name__ == "__main__":
    obj = WechatSprot(用户的openid)
    print(obj.getInfo())
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!