「原创声明:保留所有权利,禁止转载」
获取数据的方法依然采取了 regex 正则匹配的方法,请求框架采用了 java,爬虫语言是 groovy,本地拼接好 sql 语句,发送到 mysql 服务端,完成存储。
代码如下:
package com.fan
import com.fantest.httpclient.FanLibrary
import com.fantest.mysql.MySqlTest
import com.fantest.utils.Regex
import net.sf.json.JSONObject
class Company extends FanLibrary {
static void main(String[] args) {
for (def i in 1..1060) {
getPage(i)
// getInfo("/eportal/ui?pageId=307900&t=toDetail&ZSBH=D311056737")
}
testOver()
}
static getPage(int page) {
def url = "http://www.***.gov.cn/eportal/ui?pageId=307900"
def params = new JSONObject()
params.put("filter_LIKE_QYMC", EMPTY)
params.put("filter_LIKE_YYZZZCH", EMPTY)
params.put("filter_LIKE_ZSBH", EMPTY)
params.put("filter_LIKE_XXDZ", EMPTY)
params.put("currentPage", page)
params.put("pageSize", 15)
params.put("OrderByField", EMPTY)
params.put("OrderByDesc", EMPTY)
def response = getHttpResponse(getHttpPost(url, params))
def s = response.getString("content")
def all = Regex.regexAll(s, "<td s.*?浏览")
for (int i = 1; i < all.size(); i++) {
def get = all.get(i)
def regex = Regex.getRegex(get, "href=\".*?\"").replace("amp;", EMPTY)
getInfo(regex)
sleep(3)
}
return response;
}
static getInfo(String url) {
try {
url = "http://www.***.gov.cn" + url;
def response = getHttpResponse(getHttpGet(url))
def content = response.getString("content")
def all = Regex.regexAll(content, "<td class=\"label\".*?\n.*\n.*\n.*\n.*\n.*")
def name = all.get(0).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def adress = all.get(1).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def money = all.get(2).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def sid = all.get(3).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def type = all.get(4).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def man = all.get(5).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def paper = all.get(6).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def level = all.get(7).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def gov = all.get(8).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def time = all.get(9).replaceAll("<.*?>", EMPTY).replaceAll("(\n| )", EMPTY).split(":")[1]
def start = time.split("~")[0]
def end = time.split("~")[1]
String sql = "INSERT INTO company (name,adress,money,sid,type,man,paper,level,gov,start,end) VALUES (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\");"
sql = String.format(sql, name, adress, money, sid, type, man, paper, level, gov, start, end)
output(sql)
MySqlTest.sendWork(sql)
}
catch (Exception e) {
output(e)
}
}
}
第一页的网页结构如下:
第二页详情页结构如下:
第二页详情页结构如下:
regex 是我自己简单封装的正则匹配的类,代码可以去我码云上面看看
技术类文章精选
- java 一行代码打印心形
- Linux 性能监控软件 netdata 中文汉化版
- 接口测试代码覆盖率(jacoco)方案分享
- 性能测试框架
- 如何在 Linux 命令行界面愉快进行性能测试
- 图解 HTTP 脑图
- 如何测试概率型业务接口
- httpclient 处理多用户同时在线
- 将 swagger 文档自动变成测试代码
- 五行代码构建静态博客
- httpclient 如何处理 302 重定向
- 基于 java 的直线型接口测试框架初探
- Tcloud 云测平台 -- 集大成者
- 如何测试概率型业务接口
- python plotly 处理接口性能测试数据方法封装
- 单点登录性能测试方案
非技术文章精选
- 为什么选择软件测试作为职业道路?
- 成为杰出 Java 开发人员的 10 个步骤
- 写给所有人的编程思维
- 自动化测试的障碍
- 自动化测试的问题所在
- 测试之《代码不朽》脑图
- 成为优秀自动化测试工程师的 7 个步骤
- 优秀软件开发人员的态度
- 如何正确执行功能 API 测试
- 未来 10 年软件测试的新趋势 - 上
- 未来 10 年软件测试的新趋势 - 上
- 自动化测试解决了什么问题
- 17 种软件测试人员常用的高效技能 - 上
- 17 种软件测试人员常用的高效技能 - 下
大咖风采
TesterHome 为用户提供「保留所有权利,禁止转载」的选项。
除非获得原作者的单独授权,任何第三方不得转载标注了「原创声明:保留所有权利,禁止转载」的内容,否则均视为侵权。
具体请参见TesterHome 知识产权保护协议。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。