新增大头菜预测功能,使用无头浏览器抓包
This commit is contained in:
115
src/main/java/com/yutou/qqbot/utlis/WebClient.java
Normal file
115
src/main/java/com/yutou/qqbot/utlis/WebClient.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.yutou.qqbot.utlis;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.qqbot.models.Animal.TurnipProphet;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
import org.openqa.selenium.support.ui.ExpectedConditions;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class WebClient {
|
||||
private static WebClient client;
|
||||
|
||||
|
||||
public static WebClient getInstance() {
|
||||
if (client == null) {
|
||||
client = new WebClient();
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
private WebClient() {
|
||||
System.setProperty("webdriver.chrome.driver",
|
||||
"E:\\GoogleChromeDriver\\chromedriver.exe");
|
||||
java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
|
||||
}
|
||||
|
||||
public Map<String, String> openTurnip(String prices, String pattern) throws Exception {
|
||||
String url=String.format("https://turnipprophet.io?prices=%s%s",
|
||||
prices,
|
||||
pattern == null ? "" : "&pattern=" + pattern
|
||||
);
|
||||
System.out.println("url = " + url);
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||
WebDriver webDriver = new ChromeDriver(getOptions());
|
||||
|
||||
webDriver.get(url);
|
||||
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
|
||||
Thread.sleep(500);
|
||||
|
||||
WebElement element = webDriver.findElement(By.id("output"));
|
||||
List<WebElement> list = element.findElements(By.tagName("tr"));
|
||||
JSONObject pr = new JSONObject();
|
||||
JSONArray array = new JSONArray();
|
||||
|
||||
for (WebElement webElement : list) {
|
||||
String[] tmp;
|
||||
try {
|
||||
tmp = webElement.getText().replace(" ~ ", "~").split(" ");
|
||||
} catch (Exception e) {
|
||||
//e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
if ("所有趋势".equals(tmp[0])) {
|
||||
map.put(TurnipProphet.TurnipData.MONDAY_UP, tmp[3]);
|
||||
map.put(TurnipProphet.TurnipData.MONDAY_DOWN, tmp[4]);
|
||||
map.put(TurnipProphet.TurnipData.TUESDAY_UP, tmp[5]);
|
||||
map.put(TurnipProphet.TurnipData.TUESDAY_DOWN, tmp[6]);
|
||||
map.put(TurnipProphet.TurnipData.WEDNESDAY_UP, tmp[7]);
|
||||
map.put(TurnipProphet.TurnipData.WEDNESDAY_DOWN, tmp[8]);
|
||||
map.put(TurnipProphet.TurnipData.THURSDAY_UP, tmp[9]);
|
||||
map.put(TurnipProphet.TurnipData.THURSDAY_DOWN, tmp[10]);
|
||||
map.put(TurnipProphet.TurnipData.FRIDAY_UP, tmp[11]);
|
||||
map.put(TurnipProphet.TurnipData.FRIDAY_DOWN, tmp[12]);
|
||||
map.put(TurnipProphet.TurnipData.SATURDAY_UP, tmp[13]);
|
||||
map.put(TurnipProphet.TurnipData.SATURDAY_DOWN, tmp[14]);
|
||||
|
||||
map.put(TurnipProphet.TurnipData.MIX, tmp[15]);
|
||||
map.put(TurnipProphet.TurnipData.MAX, tmp[16]);
|
||||
} else {
|
||||
if (!array.toJSONString().contains(tmp[0])) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put(TurnipProphet.TurnipData.MODEL, tmp[0]);
|
||||
json.put(TurnipProphet.TurnipData.PR, tmp[1]);
|
||||
array.add(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (String key : map.keySet()) {
|
||||
System.err.println("记录最高日:"+map.get(key)+" > "+key);
|
||||
if (!key.equals(TurnipProphet.TurnipData.MAX) &&
|
||||
map.get(key).contains(map.get(TurnipProphet.TurnipData.MAX))) {
|
||||
map.put(TurnipProphet.TurnipData.DAY, key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
pr.put(TurnipProphet.TurnipData.MODEL, array);
|
||||
map.put(TurnipProphet.TurnipData.MODEL, pr.toJSONString());
|
||||
webDriver.close();
|
||||
return map;
|
||||
}
|
||||
|
||||
private ChromeOptions getOptions() {
|
||||
ChromeOptions options = new ChromeOptions();
|
||||
options.addArguments("disable-infobars");
|
||||
options.addArguments("blink-settings=imagesEnabled=false");
|
||||
options.addArguments("headless");
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user