「原创声明:保留所有权利,禁止转载」
收到一天振奋人心的假新闻,导致我去找了公开信息网站定点药店的信息,虽然结果比较失败,过程还是挺欢乐的,记录下来又可以水一篇文章了。以下是原文:
页面搜索功能有限,我就做了一个爬虫,思路分了两步,先找药店名称的编号,再查药店的具体地址。
这里面只有 773 条网点信息,我顺手做了个爬虫,代码粗糙,十几分钟完成的,效果还可以。代码分享如下:
package com.fun
import com.fun.frame.Save
import com.fun.frame.httpclient.FanLibrary
import com.fun.utils.Regex
import com.fun.utils.WriteRead
import net.sf.json.JSONObject
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
class sd extends FanLibrary {
static names = []
public static void main(String[] args) {
test2(1)
// 52.times {
// test(it+1)
// }
// Save.saveStringList(names,"hospital")
def line = WriteRead.readTxtFileByLine(LONG_Path + "hospital")
line.each {
def s = it.split(" ", 3)[1]
output(s)
it += test2(changeStringToInt(s))
names << it
}
Save.saveStringList(names,"address.txt")
testOver()
}
static def test(int i) {
String url = "http://ybj.beijing.gov.cn/ddyy/ddyy2/list";
HttpPost httpPost = getHttpPost(url, getJson("page=" + i));
JSONObject response = getHttpResponse(httpPost);
def all = response.getString("content").replaceAll("\\s", EMPTY)
def infos = Regex.regexAll(all, "<tr>.*?</tr>")
infos.remove(0)
output(infos)
output(infos.size())
infos.each {x ->
names << x.substring(4).replaceAll("<.*?>", SPACE_1)
}
output(names.size())
}
static def test2(int i) {
String url = "http://ybj.beijing.gov.cn/ddyy/ddyy2/findByName?id="+i;
HttpGet httpGet = getHttpGet(url);
JSONObject response = getHttpResponse(httpGet);
def all = response.getString("content").replaceAll("\\s", EMPTY)
def infos = Regex.regexAll(all, "<tr>.*?</tr>")
// infos.remove(0)
output(infos)
// output(infos.size())
def address = EMPTY
infos.each {x ->
// names << x.substring(4).replaceAll("<.*?>", SPACE_1)
address += x.substring(4).replaceAll("<.*?>", SPACE_1)
}
output(address)
return address
}
}
下面是两次爬虫的页面结构:
篇幅有限,我已经整理了 Excel 文档。需要的小伙伴添加微信索要。
- 郑重声明:文章首发于公众号 “FunTester”,禁止第三方(腾讯云除外)转载、发表。
技术类文章精选
- Linux 性能监控软件 netdata 中文汉化版
- 性能测试框架第三版
- 如何在 Linux 命令行界面愉快进行性能测试
- 图解 HTTP 脑图
- 将 swagger 文档自动变成测试代码
- Selenium 4.0 Alpha 更新实践
- 如何统一接口测试的功能、自动化和性能测试用例
非技术文章精选
- 写给所有人的编程思维
- 成为自动化测试的 7 种技能
- Web 端自动化测试失败原因汇总
- 测试人员常用借口
- API 测试基础
- API 自动化测试指南
- 未来的 QA 测试工程师
- JSON 基础
- 2020 年 Tester 自我提升
TesterHome 为用户提供「保留所有权利,禁止转载」的选项。
除非获得原作者的单独授权,任何第三方不得转载标注了「原创声明:保留所有权利,禁止转载」的内容,否则均视为侵权。
具体请参见TesterHome 知识产权保护协议。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。