案列说明:
分类:外卖平台
案例:饿了么平台信息抓取
业务流程描述:
【1】业务描述
【2】元素技术定位
抓取方法论:
【1】【找】找到URL地址。
【2】【定】确定每页面的抓取的元素。
【3】【编】瑞雪采集云编码。
编码实现:
package com.rx.crawler.open.emm;
import java.util.List;
import com.ruixuesoft.crawler.open.Keys;
import com.ruixuesoft.crawler.open.RxCrawler;
import com.ruixuesoft.crawler.open.RxCrawlerException;
import com.ruixuesoft.crawler.open.RxDatabase;
import com.ruixuesoft.crawler.open.RxNode;
import com.ruixuesoft.crawler.open.RxResult;
import com.ruixuesoft.crawler.open.RxRule;
import com.ruixuesoft.crawler.open.RxTask;
public class EmmRule1 implements RxRule {
private RxCrawler crawler = null;
private String homeUrl = "https://www.ele.me/home/";
private RxNode currentNode = null;
@Override
public RxResult execute(RxTask task, RxCrawler crawler, RxDatabase database)
throws RxCrawlerException {
this.crawler = crawler;
this.searchOp();
this.login();
this.selectType();
return new RxResult(200);
}
private void searchOp() {
this.crawler.open(this.homeUrl);
this.crawler.sleepSeconds(1);
String cityXpath = "/html/body/div/div/div/div[2]/div[1]/div[1]/a";
this.currentNode = this.crawler.getNodeByXpath(cityXpath);
this.currentNode.click();
String inputCityXpath = "/html/body/div/div/div/div[2]/div[1]/div[1]/div/div/div[2]/div[2]/input";
this.currentNode = this.crawler.getNodeByXpath(inputCityXpath);
this.currentNode.input("上海");
String lengentNode = "/html/body/div/div/div/div[2]/div[1]/div[1]/div/div/div[2]/div[2]/ul/li";
this.currentNode = this.crawler.getNodeByXpath(lengentNode);
this.currentNode.click();
String AddXpath = "/html/body/div/div/div/div[2]/div[1]/div[2]/form/input";
this.currentNode = this.crawler.getNodeByXpath(AddXpath);
this.currentNode.input("上海市徐汇区罗秀路1115-1号");
String lengXpath = "/html/body/div/div/div/div[2]/div[1]/div[2]/div/ul/li/p[1]";
this.currentNode = this.crawler.getNodeByXpath(lengXpath);
this.currentNode.click();
}
private void login() {
String longinXpath = "/html/body/div[4]/div[3]/div[2]/div[3]/a";
this.currentNode = this.crawler.getNodeByXpath(longinXpath);
this.currentNode.click();
String passWordXpath = "/html/body/div[1]/div[1]/div[1]/div/a[2]";
this.currentNode = this.crawler.getNodeByXpath(passWordXpath);
this.currentNode.click();
String namePath = "/html/body/div[1]/div[1]/div[2]/form/section[1]/input";
this.currentNode = this.crawler.getNodeByXpath(namePath);
this.currentNode.input("15566898605");
String passPath = "/html/body/div[1]/div[1]/div[2]/form/section[2]/input";
this.currentNode = this.crawler.getNodeByXpath(passPath);
this.currentNode.input("1q22w#22E4r0221");
String longinXpath1 = "/html/body/div[1]/div[1]/div[2]/form/button";
this.currentNode = this.crawler.getNodeByXpath(longinXpath1);
this.currentNode.click();
this.crawler.back();
this.crawler.back();
}
private void type(String type) {
// String type1 = "/html/body/div[4]/div[3]/div[1]/div/a[8]";
this.currentNode = this.crawler.getNodeByXpath(type);
this.currentNode.click();
String xpath = "//div[@class='rstblock-content']";
this.crawler.pressKeys(1, Keys.PAGE_DOWN);
this.crawler.sleepSeconds(1);
this.crawler.pressKeys(1, Keys.PAGE_DOWN);
this.crawler.sleepSeconds(1);
this.crawler.pressKeys(1, Keys.PAGE_DOWN);
this.crawler.sleepSeconds(1);
List<RxNode> nodeList = this.crawler.getNodeListByXpath(xpath);
System.out.println("size->" + nodeList.size());
nodeList.forEach(rxnode -> System.out.println(rxnode.getText()));
this.crawler.sleepSeconds(5);
}
private void selectType() {
// 美食
System.out.println("-----------------美食-----------------");
this.type("/html/body/div[4]/div[3]/div[1]/div/a[2]");
System.out.println("-----------------美食-----------------");
// 果蔬生鲜
System.out.println("-----------------果蔬生鲜-----------------");
this.type("/html/body/div[4]/div[3]/div[1]/div/a[8]");
System.out.println("-----------------果蔬生鲜-----------------");
// 商店超市
System.out.println("-----------------商店超市-----------------");
this.type("/html/body/div[4]/div[3]/div[1]/div/a[9]");
System.out.println("-----------------商店超市-----------------");
// 鲜花绿植
System.out.println("-----------------鲜花绿植-----------------");
this.type("/html/body/div[4]/div[3]/div[1]/div/a[10]");
System.out.println("-----------------鲜花绿植-----------------");
// 医药健康
System.out.println("-----------------医药健康-----------------");
this.type("/html/body/div[4]/div[3]/div[1]/div/a[11]");
System.out.println("-----------------医药健康-----------------");
}}
总结:
【1】瑞雪采集云外卖业务抓取。
【2】瑞雪采集云业务流程处理过程。
【3】Xpath技术相对路径。