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. utils工具类

    使用工具类的方法 目录结构 o代表项目层 util.js 用promise避免异步获取不到数据的问题 注意module.exports var APIURL = ''; function getApi ...

  2. 【转】rem自适应布局

    rem自适应原理 rem是根据html的font-size大小来变化,正是基于这个出发,我们可以在每一个设备下根据设备的宽度设置对应的html字号,从而实现了自适应布局.更多介绍请看这篇文章:rem是 ...

  3. python发送短信和发送邮件

    先注册好 发短信脚本内容 #接口类型:互亿无线触发短信接口,支持发送验证码短信.订单通知短信等. #账户注册:请通过该地址开通账户http://sms.ihuyi.com/register.html ...

  4. 19.1 PORT CONTROL DESCRIPTIONS

    [原文] PORT CONFIGURATION REGISTER (GPACON-GPJCON) In S3C2440A, most of the pins are multiplexed pins. ...

  5. 解决腾讯云封锁SS(shadow--socks)访问google问题

    腾讯云windows主机,若使用SS(shadow--socks)采用加密算法aes-256-cfb,访问google的时候会被dropped connection. 解决办法更改ss server加 ...

  6. php7 pdo抽象类操作数据库

    查询 <?php try { $dbconnect = new PDO('mysql:host=localhost;dbname=pdodatabase','root','753951'); } ...

  7. windows下 go安装qt绑定

    1.下载安装QT 离线版QT地址:  https://download.qt.io/official_releases/qt/5.11/5.11.1/qt-opensource-windows-x86 ...

  8. 学习笔记之X分钟速成Python3

    X分钟速成Python3 https://mp.weixin.qq.com/s/QT5sR0nUKgJYsYgrj2SleA https://learnxinyminutes.com/docs/zh- ...

  9. 使用uiautomator2进行webview页面的测试

    1.开发开启webview debug模式 2.使用VirtualXposed框架进行webview测试,详细见https://testerhome.com/topics/16156 下载,安装Vir ...

  10. go语言学习--go中godep的使用小结

    go中的godep 本文参考:http://www.cnblogs.com/me115/p/5528463.html#h20 http://studygolang.com/articles/4385 ...