「原创声明:保留所有权利,禁止转载」
内容爬虫完毕,校验完毕,缺失信息暂未统计。总数据 720 万,地区 3200 个,年份从 2011-2018,大小 950Mb,原始数据已丢失,需要的朋友可以自己运行脚本挂一晚上。中间遇到了很多坑,有机会我再写一遍博客专门讲讲大量数据爬虫遇到的坑。
本人在使用基于 java 的脚本语言 groovy 做爬虫时,用了获取城市历史天气的任务做练习。数据源隐藏了,有需要的我可以直接发数据。使用过程中虽然有些绊脚石,总体来说还是很良好的,脚本语言 groovy 相比 java 的确省事儿很多。分享代码,供大家参考。(城市对应的编码存在一个 js 里面了,这里不写)
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-2019年数据
* @param cityId
*/
static getCityAll(int cityId) {
for (int j in 2011..2019) {
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 一行代码打印心形
- Linux 性能监控软件 netdata 中文汉化版
- 接口测试代码覆盖率(jacoco)方案分享
- 性能测试框架
- 如何在 Linux 命令行界面愉快进行性能测试
- 图解 HTTP 脑图
- 将 swagger 文档自动变成测试代码
- 五行代码构建静态博客
- 基于 java 的直线型接口测试框架初探
非技术文章精选
- 为什么选择软件测试作为职业道路?
- 写给所有人的编程思维
- 自动化测试的问题所在
- 成为优秀自动化测试工程师的 7 个步骤
- 未来 10 年软件测试的新趋势 - 上
- 未来 10 年软件测试的新趋势 - 上
- 手动测试存在的重要原因
- 成为自动化测试的 7 种技能
大咖风采
TesterHome 为用户提供「保留所有权利,禁止转载」的选项。
除非获得原作者的单独授权,任何第三方不得转载标注了「原创声明:保留所有权利,禁止转载」的内容,否则均视为侵权。
具体请参见TesterHome 知识产权保护协议。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。