読者です 読者をやめる 読者になる 読者になる

kimonoでスクレイピング

Program Python スクレイピング kimono

Kimono : Turn websites into structured APIs from your browser in seconds
kimonoで今治の不審者情報JSON

改行と全角数字を整形

import json
import urllib.request
import unicodedata

results = json.loads(urllib.request.urlopen("https://www.kimonolabs.com/api/4hckzthq?apikey=Hpo3uxKTJJA2W55lcnD7uHgvZ8shKiKQ").read().decode("utf-8"))

for fusin in results['results']['Suspicious_Person'][0:10]:

    #dateの数字を半角に統一
    f_date = unicodedata.normalize('NFKC', fusin['date'].replace('\n',''))
    f_week = fusin['week']
    f_spot = fusin['spot'].replace('\n','')
    f_victim = fusin['victim'].replace('\n','')
    f_suspicious_person = fusin['suspicious_person'].replace('\n','')
    f_description = fusin['description']
  
    print(f_date, f_week, f_spot, f_victim, f_suspicious_person, f_description)

kimono Java Script

function transform(data) {
  function clean(user) {
    //改行削除
    user.date = user.date.replace(/\n/g, '');
    user.spot = user.spot.replace(/\n/g, '');
    user.suspicious_person = user.suspicious_person.replace(/\n/g, '');
    user.victim = user.victim.replace(/\n/g, '');

    //全角数字を半角数字に変換
    user.date = user.date.replace(/[A-Za-z0-9]/g, function(s) {
      return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
    });

    return user;
  }

  for (var collection in data.results) {
    data.results[collection] = data.results[collection].map(clean);
  }

  return data;
}