抓取高速公路摄像照片

抓取某省所有高速公路上摄像头所拍下的照片,并保存到本地。

根据抓包每张拍摄到的照片的链接地址,可以知道,摄像头的设备id,是照片链接中最重要的参数。同时也可以查看网页加载的js文件中,后台程序是怎样构造照片的链接地址。

所以第一步是获取所有 摄像头的 deviceID,
然后构造照片的链接。
下载照片的二进制数据,保存到本地为照片。

#coding:utf-8
import json
import requests
import os
import time
from datetime import datetime
import re

class getCameraID:
    def __init__(self):
        self.url = "http://url----/gzcx/map/getAllCameraInfo.do"

    def getID(self):
        resp = requests.get(self.url) 
        data = json.loads(resp.text)  #resp.text 文本响应
        # print len(data)
        ID_list = []
        for i in xrange(len(data)):
            ID_list.append(data[i].get("deviceid",""))
        return ID_list

class getPic:
    def __init__(self,cameraID_list):
        self.pre_url = "http://url----/gzcx/camera/"
        self.url_list = []
        for camera_id in cameraID_list:
            self.url_list.append(self.pre_url+camera_id+".bmp")

    def getPicture(self, path="./temp"):
        t_note = datetime.now()
        time_note = t_note.strftime("%F_%H-%M-%S")
        path = path + time_note
        path = path.strip()
        if not os.path.exists(path):
            os.mkdir(path)

        i = 0
        for url in self.url_list:
            resp = requests.get(url)
            data = resp.content  #二进制响应
            save_file(name=str(i)+".jpg", data=data, path=path)
            i += 1

def save_file(name,data,path):

    f = open(path+"/"+name, 'wb')
    f.write(data)
    f.flush()
    f.close()

if __name__ == "__main__":
    start_time = time.clock()
    start_time1 = time.asctime(time.localtime(time.time()))
    print "cpu time", start_time
    print "start_time1",start_time1

    cameraID = getCameraID()
    cameraID_list = cameraID.getID()

    getPicture = getPic(cameraID_list)
    getPicture.getPicture()

    end_time = time.clock()
    print "cpu time 2", end_time
    end_time1 = time.asctime(time.localtime(time.time()))
    print "end_time1", end_time1

其中,以上的url地址没具体写明
python 2.7

你可能感兴趣的:(python,爬虫)