抓取网页数据工具(极尽其所能将网页内容、HTTP报头、Cookie里的数据记录 )
优采云 发布时间: 2022-02-08 04:09抓取网页数据工具(极尽其所能将网页内容、HTTP报头、Cookie里的数据记录
)
GoldDataSpider 是一个用于抓取网页和提取数据的工具。其核心代码与黄金数据采集融合平台分离。
该项目提供从网页中抓取和提取数据的功能。它不仅可以提取网页内容,还可以从 URL、HTTP 标头和 cookie 中提取数据。
该项目定义了简洁、灵活和敏捷的结构或常规语法。尽最大努力从网页内容、HTTP 标头、cookie 中提取有意义和有价值的数据字段,甚至将其他网页和其他 网站 数据关联起来形成数据记录。此外,还可以嵌入http请求来补充数据字段,比如一些需要提供字典翻译的字段等等。
该项目还支持从各种类型的文档中提取数据,例如html/xml/json/javascript/text等。
我们还提供规则可视化公式,请下载。也
入门
首先,我们需要给项目添加依赖,如下:
1、对于 maven 项目
com.100shouhou.golddata
golddata-spider
1.1.3
2、对于 gradle 项目
compile group: 'com.100shouhou.golddata', name: 'golddata-spider', version: '1.1.3'
然后就可以使用这个依赖提供的简洁明了的API,如下:
@Test
public void testGoldSpider(){
String ruleContent=
" { \n"+
" __node: li.sky.skyid \n"+
" date: \n"+
" { \n"+
" expr: h1 \n"+
" __label: 日期 \n"+
" } \n"+
" sn: \n"+
" { \n"+
" \n"+
" js: md5(baseUri+item.date+headers['Content-Type']);\n"+
" } \n"+
" weather: \n"+
" { \n"+
" expr: p.wea \n"+
" } \n"+
" temprature: \n"+
" { \n"+
" expr: p.tem>i \n"+
" } \n"+
" } \n";
GoldSpider spider= com.xst.golddata.GoldSpider.newSpider()
.setUrl("http://www.weather.com.cn/weather/101020100.shtml")
.setRule(ruleContent)
.request();
List list=spider.extractList();
// List weathers=spider.extractList(Weather.class);
// Weather weathers=spider.extractFirst(Weather.class);
list.forEach( System.out::println);
}
运行上面的测试,你会看到类似下面的输出:
{date=19日(今天), weather=阴转小雨, temprature=10℃, sn=8bc265cb2bf23b6764b75144b255d81d}
{date=20日(明天), weather=小雨转多云, temprature=11℃, sn=9efd7e7bbbfb9bb06e04c0c990568bfd}
{date=21日(后天), weather=多云转中雨, temprature=11℃, sn=728539ac882721187741708860324afa}
{date=22日(周六), weather=小雨, temprature=9℃, sn=a23fa2233e750a3bdd11b2e200ed06c3}
{date=23日(周日), weather=小雨转多云, temprature=8℃, sn=b27e1b8a8e92a7bed384ceb3e4fdfb5f}
{date=24日(周一), weather=多云转小雨, temprature=8℃, sn=c142b7fd12330ca031dd96b307c0d50d}
{date=25日(周二), weather=小雨转中雨, temprature=6℃, sn=16f71d3c8f09394588532a3ed1a8bacf}
用作服务或 API
您可以在项目中将其用作调用服务和 API。例如如下:
@Service
public class WeatherServiceImpl implements WeatherService{
public List listByCityId(Long cityId){
String url="http://www.weather.com.cn/weather/"+cityId+".shtml"
String rule=""
GoldSpider spider= com.xst.golddata.GoldSpider.newSpider()
.setUrl(url)
.setRule(ruleContent)
.request();
return spider.extractList(Weather.class);
}
}