java不可以爬数据吗_JAVA爬数据也是杠杠的,看我爬下杜大哥

阅读文本大概需要6分钟。

上篇分析的网站是国家级,没有真正编写代码爬取对应的数据,今天以“1药网”为例来爬一爬药品数据

https://www.111.com.cn/

1、分析网站

进入网站首页

c5d63b9ee54f5f0e00853af72bdb0bf6.png

2、点击一下“所有商品分类”,对应的网站地址如下

https://www.111.com.cn/categories/

3311db47a86dffc4b21f8809bdb8cea6.png

先爬取所有的“分类”,然后在根据“分类”获取分类下的所有商品。

发起Get请求的方法

public staticStringsendGet(String url){

String result =null;//CreatesCloseableHttpClient instance with default configuration.CloseableHttpClienthttpCilent = HttpClients.createDefault();HttpGethttpGet =newHttpGet(url);

try{

CloseableHttpResponse response =httpCilent.execute(httpGet);Stringresult = EntityUtils.toString(response.getEntity());System.out.println(result);}catch(IOException e) {

e.printStackTrace();}finally{try{

httpCilent.close();//释放资源}catch(IOException e) {

e.printStackTrace();}

}returnresult;}

获取“药品分类”的HTML页面

public staticStringgetCategories(){

String html =  HttpUtils.sendGet("https://www.111.com.cn/categories/");

returnhtml;}

3、分析爬取到的药品分类的HTML,认真看图

a5f3c10301dc046acdb615dea4a0b868.png

把界面上所有的分类链接解析出来

4、解析所有分类链接

5、public staticListprocessCategoriesHtml(String html){

List links =newArrayList();

if(html!=null&& !"".equals(html.trim())){try{

Parser parser =newParser(html);//定义一个Filter,过滤主题aNodeFilter afilter =newNodeClassFilter(LinkTag.class);//A过滤器NodeList nodeList =  parser.extractAllNodesThatMatch(afilter);

for(inti=0;i

Node aNode =  nodeList.elementAt(i);LinkTag aLinkTag =  (LinkTag)aNode;

if(aLinkTag.getLink()!=null&& aLinkTag.getLink().contains("categories")){

links.add(aLinkTag.getLink());System.out.println(aLinkTag.getLink());}

}

}catch(Exception e){

e.printStackTrace();}

}returnlinks;}

6、下面来看看“杜蕾斯”的兄弟没到底有多少

在第五步爬出来的连接有如下

//www.111.com.cn/categories/965327-j1

一个连接,同样先爬取HTML页面

public staticStringgetDLS_Html(){

String html =  HttpUtils.sendGet("https://www.111.com.cn/categories/965327-j1");

returnhtml;}

分析获取到HTML

55b60827479c5c6067500f83c18719c9.png

每个“杜大哥”的连接都有product和class="product_pic pro_img"。用如下代码即可获取所有的“杜蕾斯”兄弟的商品链接

public staticListprocessDLSHtml(String html){

List links =newArrayList();

if(html!=null&& !"".equals(html.trim())){try{

Parser parser =newParser(html);//定义一个Filter,过滤主题emNodeFilter afilter =newNodeClassFilter(LinkTag.class);//A过滤器NodeList nodeList =  parser.extractAllNodesThatMatch(afilter);

for(inti=0;i

Node aNode =  nodeList.elementAt(i);LinkTag aLinkTag =  (LinkTag)aNode;

if(aLinkTag.getLink()!=null&& aLinkTag.getLink().contains("product")){

links.add(aLinkTag.getLink());System.out.println(aLinkTag.getLink());}

}

}catch(Exception e){

e.printStackTrace();}

}returnlinks;}

7、接下来就可以获取所有的商品规格了,哪款卖的多都可以分析出来哦

同样先获取页面详情

public staticStringgetDLSDetail_Html(String url){

String html =  HttpUtils.sendGet("https:"+url);

returnhtml;}

然后分析HTML可以知道只要分析下图这个div的信息就可以获取“杜大哥”的所有信息了。这个留给大家分析分析下看看怎获取。

e0735dfe17125c38a10a939a0549143e.png

关注我

每天进步一点点

很干!在看吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值