package com.fan

import com.fission.source.httpclient.ApiLibrary
import com.fission.source.httpclient.FanRequest
import com.fission.source.mysql.MySqlTest
import com.fission.source.source.WriteRead
import com.fission.source.utils.Log
import net.sf.json.JSONException
import net.sf.json.JSONObject

class Weather extends ApiLibrary {

/**
* 获取城市2011-2018年数据
* @param cityId
*/
static getCityAll(int cityId) {
for (int j in 2011..2018) {
getCityYear(cityId, j)
sleep(1000 + getRandomInt(1000))
}
}

/**
* 获取当年的数据
* @param cityId
* @param year
*/
static getCityYear(int cityId, int year) {
for (int i in 1..12) {
if (year == 2018 && i > 9) continue
getMonth(cityId, year, i)
sleep(1000 + getRandomInt(1000))
}
}
/**
* 获取某个城市某一年某一月的数据
* @param cityId
* @param year
* @param month
*/
static getMonth(int cityId, int year, int month) {
def yyyymm;
def uri;
if (year > 2016) {
yyyymm = year * 100 + month
uri = "http://tianqi.***.com/t/wea_history/js/" + yyyymm + "/" + cityId + "_" + yyyymm + ".js"
} else {
yyyymm = year + EMPTY + month
uri = "http://tianqi.***.com/t/wea_history/js/" + cityId + "_" + yyyymm + ".js"
}
output(uri)
def response = FanRequest.isGet()
.setUri(uri)
.getResponse()
.getString("content")
.substring(16)
.replace(";", EMPTY)
def weather = JSONObject.fromObject(response)
def city = weather.getString("city")
def array = weather.getJSONArray("tqInfo")
output(array.size())
for (int i in 0..array.size() - 1) {
JSONObject info = array.get(i)
if (!info.containsKey("ymd")) continue
def date = info.getString("ymd")
def low = info.getString("bWendu").replace("℃", EMPTY)
def high = info.getString("yWendu").replace("℃", EMPTY)
def wea = info.getString("tianqi")
def wind = info.getString("fengxiang")
def fengli = info.getString("fengli")
def aqi = TEST_ERROR_CODE, aqiInfo = EMPTY, aqiLevel = TEST_ERROR_CODE;
if (info.containsKey("aqi")) {
aqi = info.getInt("aqi")
aqiInfo = info.getString("aqiInfo")
aqiLevel = info.getInt("aqiLevel")
}
String sql = "INSERT INTO weather (city,low,high,date,wind,windsize,weather,aqi,aqilevel,aqiinfo) VALUES (\"%s\",%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",%d,%d,\"%s\");"
sql = String.format(sql, city, changeStringToInt(low), changeStringToInt(high), date, wind, fengli, wea, aqi, aqiLevel, aqiInfo)
output(sql)
MySqlTest.sendWork(sql)
}
}

Java学习随笔(1)--groovy爬虫的更多相关文章

  1. (。・・)ノ~个人java学习随笔记录

    基本认识 1.编程思维 根据这几天的java学习,编写程序最重要的就是要有一个清晰的思路.语法上的错误可以跟随着不断的联系与学习来弥补,清晰的思维却只有自己来前期模仿,后面慢慢摸索形成一套属于自己的思 ...

  2. Java学习随笔(2)--爬虫--天气预报

    public class Spiderweather { public static void main(String[] args) { List<String> list = null ...

  3. JAVA 学习随笔 : JDK Enhancement Process JEP process

    是时候寻找一个学习JAVA的路径了 ---- JDK Enhancement Process Oracle发布了JDK增强提案与路线图进程,目的在于鼓励OpenJDK提交者贡献点子和扩展以改进Open ...

  4. java学习随笔--- 捣蛋vector

    最近比较有时间啦,有时间搞下java,个人觉得学这门语言语法太多啦,不一一去学习啦,心血来潮,挂了个struct2的源代码,一入深似海啊,看得我天花缭乱,从最简单的开始吧 public static ...

  5. Java学习随笔5:Java多线程编程

    1. 线程是程序中单独顺序的控制流,线程本身依靠程序进行运行,线程是程序中的顺序控制流,只能使用分配给程序的资源和环境. 2. 进程是执行中的程序,一个进程可以包含一个或多个线程,但至少要包含一个线程 ...

  6. Java学习随笔4:Java的IO操作

    1. IO流的作用是读写设备上的数据,如硬盘文件.内存.键盘.网络等.根据数据走向,可分为:输入流和输出流:根据处理的数据类型,可分为:字节流和字符流.字节流可以处理所有类型的数据,如MP3.图片.视 ...

  7. Java学习随笔3:遍历文件夹及文件的读取和写入

    import java.io.File; /** * 遍历文件夹 */ public class ScannerFile { public static void main(String[] args ...

  8. Java学习随笔2:Java复合赋值表达式的小问题

    问题:i += j只是i = i + j的简写么? 答案:非也!看下面的程序: int i = 5; long j = 8; i += j; // 可以通过编译且结果正确 i = i + j; // ...

  9. Java学习随笔1:Java是值传递还是引用传递?

    Java always passes arguments by value NOT by reference. Let me explain this through an example: publ ...

随机推荐

  1. MongoDB关键指标意义&各数值区间意义&部署

    ## part 1 mms图 What's MMS MongoDB Management Service (MMS) is a suite of services for managing Mongo ...

  2. 第五章JavaScript

    创建数组://1.字面量方式创建 (推荐大家使用这种方式创建数组 简单粗暴) var colors = ['red','color','yellow'];console.log(colors) //空 ...

  3. 钱管够,你能接这个项目吗?+ tomcat源码分析

    最近看了几个咕泡学院的公开课,课堂老师讲到下面这两个经历. 1:钱给够,你有没有能力接下这个全国性的项目 平时也会有怀才不遇的时候,但是当你遇到这个机会的时候,有没有信心去接下这个单子呢? 信心和能力 ...

  4. [原] inline operator delete & DLL boundary

    很久以前写在百度空间的这篇文章: [百度空间] [原] 全局operator delete重载到DLL 首先,纠正一个词“重载”,operator new/delete是替换(replacement) ...

  5. 使用dotenv 管理nodejs 应用的环境变量&&docker-compose 运行

      说明dotenv 是一个很方便的符合12 factor 的环境变量管理工具,使用很方便,实际上里面的代码也不是很多 测试使用docker 进行环境部署,为了方便分发使用pkg 进行打包,使用alp ...

  6. Unity3D外包 团队更新一下UE4和Unity3D案例

    欢迎联系我们索取,谢谢! 有项目外包请联系QQ:372900288 索取案例.

  7. 【转】Mysql索引设计原则

    来源:https://segmentfault.com/a/1190000000473085 假设一高频查询如下SELECT * FROM user WHERE area='amoy' AND sex ...

  8. thymeleaf的fragment例子

    fragment介绍 fragment类似于JSP的tag,在html中文件中,可以将多个地方出现的元素块用fragment包起来使用. 定义fragment 新建foot.html文件 <!D ...

  9. Optaplanner终于支持多线程并行运行 - Multithreaded incremental solving

    Optaplanner 7.9.0.Final之前,启动引擎开始对一个Problem进行规划的时候,只能是单线程进行的.也就是说,当引擎对每一个possible solution进行分数计算的过程中, ...

  10. python调试pdb

    开始调试 python3 -m pdb pdb.py break 或 b line_num 设置断点设置断点 continue 或 c继续执行程序 list 或 l查看当前行的代码段 step 或 s ...