裁判文书网APP数据的抓取

       第一次爬取APP数据记录一下,裁判文书网APP数据的抓取,可以获得数据,但是是加密过之后的数据。下边是源码:


import uuid
import datetime
import requests
import json
import hashlib
import random
from random import choice
from court.ip_lists import ip_lists
import time
import pymysql
def devid():
    devid=str(uuid.uuid1()).replace('-','')
    return devid
def timespan():
    timespan=datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    return timespan
def nonce():
    nonce = []
    s = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
         'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    for i in range(0, 4):
        a = random.choice(s)
        nonce.append(a)
    non = str(nonce).replace("[", "").replace("]", "").replace("'", "").replace(",", "").replace(" ", "")
    return non
def signature(timespan,nonce,devid):
    s = timespan+ nonce + devid
    mid = hashlib.md5(s.encode(encoding='utf-8')).hexdigest()
    return mid
def token():
    times = timespan()
    dev = devid()
    nonc = nonce()
    url='http://wenshuapp.court.gov.cn/MobileServices/GetToken'
    headers={
    'Content-Type': 'application/json',
    'timespan': timespan(),
    'nonce': nonce(),
    'devid': devid(),
    'signature':signature(times,nonc,dev) ,
    'User-Agent': 'Dalvik/1.6.0 (Linux; U; Android 4.4.2; vivo X20A Build/NMF26X)',
    'Host': 'wenshuapp.court.gov.cn',
    'Connection': 'Keep-Alive',
    'Accept-Encoding': 'gzip',
    # 'Content-Length': '71'
    }
    data='{"app":"cpws","devid":"%s","apptype":"1"}'%(devid)
    html=requests.post(url=url,headers=headers,data=data,proxies={'http':choice(ip_lists)})
    token=json.loads(html.text)['token']
    print(token)
    return token

def parse():
    while 1:
        try:

            url='http://wenshuapp.court.gov.cn/MobileServices/GetAddCountAndTotalAndPVCount'
            times=timespan()
            dev=devid()
            nonc= nonce()
            toke=token()
            headers={
                'Content-Type': 'application/json',
                'timespan': times,
                'nonce':nonc,
                'devid': dev,
                'signature':signature(times,nonc,dev),
                'User-Agent': 'Dalvik/1.6.0 (Linux; U; Android 4.4.2; vivo X20A Build/NMF26X)',
                'Host': 'wenshuapp.court.gov.cn',
                'Connection': 'Keep-Alive',
                'Accept-Encoding': 'gzip',
                'Content-Length': '60'

            }
            data='{"app":"cpws","reqtoken":"%s"}' % toke
            data = data.encode('utf-8')
            html=requests.post(url=url,headers=headers,data=data,timeout=6)
            if html.text=='':
                print('html.text=null')
                time.sleep(2)
                continue
            print(html.text)
            return
        except Exception as e:
            print(e)
            continue



def stare_request():
    i=0
    while i<118876:
        try:
            times = timespan()
            dev=devid()
            nonc = nonce()
            headers={
                'Content-Type': 'application/json',
                'timespan': times,
                'nonce': nonc,
                'devid':dev,
                'signature':signature(times,nonc,dev),
                'User-Agent': 'Dalvik/1.6.0 (Linux; U; Android 4.4.2; vivo X20A Build/NMF26X)',
                'Host': 'wenshuapp.court.gov.cn',
                'Connection': 'Keep-Alive',
                'Accept-Encoding': 'gzip',
                # 'Content-Length': '242'
            }
            toke=token()
            data = '{"dicval":"asc","reqtoken":"%s",' \
                   '"condition":"/CaseInfo/案/@上传日期=[2018-06-21 TO 2018-06-22]",' \
                   '"skip":"%s","app":"cpws","limit":"20","dickey":"/CaseInfo/案/@法院层级"}' % (toke, str(i))
            data=data.encode('utf-8')
            url='http://wenshuapp.court.gov.cn/MobileServices/GetLawListData'
            html=requests.post(url=url,headers=headers,data=data ,timeout=6,proxies={'http':choice(ip_lists)})
            if html.text == ''  :
                print('html.text=null')
                time.sleep(0.5)
                continue
            elif 'Request Error' in html.text:
                print('Request Error')
                time.sleep(0.5)
                continue
            elif html.status_code!=200:
                continue
            print('爬取到第几条数据:',i)
             
            i+=20
        except Exception as e:
            print(e)
            continue
if __name__ == '__main__':
    parse()
    stare_request()

       在请求的时候一次不一定能请求到,这个需要多请求几次就可以获得数据了。

数据的解密关键字段是toke,times,uuid和返回的数据了。解密的方法就不多说了。

但是在爬取的时候感觉还是爬取web版的要快一点,可能还是因为对APP这个不是太熟悉的吧。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
爬虫(Web Crawler)是一种自动化程序,用于从互联上收集信息。其主要功能是访问页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对站造成过大负担或触发反爬虫机制,爬虫需要遵守站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重站的使用政策,并确保对被访问站的服务器负责。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值