Selenium解析Json格式数据
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
添加Fastjson.jar
public static void main(String arg[])
{
String url = "http://10.19.90.232:9080/recommend-portal/recommendv2/biz.jsonp?parameter=iphone&cityId=9173&sceneIds=2-1&count=20";
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to(url);
Window window = driver.manage().window();
// window.maximize();
String html = driver.getPageSource();
System.out.println("html字符串内容= " + html);
String strHead = "
";";
String strLast = "
String jsonStr = html.substring(strHead.length(), html.length() - strLast.length());
System.out.println("json字符串内容= " + jsonStr);
JSONObject jsonObj = JSONObject.parseObject(jsonStr);
JSONArray jsonArrSugGoods = jsonObj.getJSONArray("sugGoods");
JSONArray jsonArrSkus = jsonArrSugGoods.getJSONObject(0).getJSONArray("skus");
System.out.println("json字符串的【sugGoods】 数组大小= " + jsonArrSugGoods.size());
System.out.println("json字符串的【skus】 数组大小= " + jsonArrSkus.size());
for (int i = 0; i < jsonArrSkus.size(); i++)
{
// System.out.println("【skus】的【handwork】值:=" +
// jsonArrSkus.getJSONObject(i).getString("handwork"));
if (jsonArrSkus.getJSONObject(i).getString("handwork") == "01A")
{
// System.out.println("[handwork]值:=" +
// jsonArrSkus.getJSONObject(i).getString("handwork"));
System.out.println("此为人工数据!");
} else
{
System.out.println("此为算法数据!");
}
}
}
Json数据处理jar包及代码:
package suning.searchresult;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class SearchIndex {
public static void main(String[] args){
int counta = 0;
int countb = 0;
int c = 1000;
String url = "http://10.19.90.232:9080/recommend-portal/recommendv2/biz.jsonp?parameter=000000000104430621&cityId=9173&sceneIds=1-2&count=10"
;
//如果FireFox浏览器未安装在C盘,则应该还要加上如下代码:
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to(url);
Window window = driver.manage().window();
window.maximize();
for(int j = 0 ; j < c ; j++){
driver.navigate().refresh();
String html = driver.getPageSource();
//System.out.println(html);
//System.out.println(html.substring(69, 77));
if(!html.substring(69, 77).equals("sugGoods")){
System.out.println("第"+(j+1)+"次打开浏览器失败");
}else{
System.out.println("第"+(j+1)+"次打开浏览器成功");
}
//返回的json串其实是包含html标记的,返回要处理后获得json串。
String s1 = "
{\"sugGoods\":[";";
String s2 = "]}
String s = html.substring(s1.length(), html.length()-s2.length());
JSONObject myObj = JSONObject.parseObject(s);
JSONArray myArray = myObj.getJSONArray("skus");
int a=0;
int b=0;
String flaga = "01A";
String flagb = "01B";
for(int i=0;i
//System.out.println(myArray.getJSONObject(i).get("handwork"));
//json要一级一级地解析才会成功。
if(myArray.getJSONObject(i).get("handwork").equals(flaga)){
a++;
}else if(myArray.getJSONObject(i).get("handwork").equals(flagb)){
b++;
}else{
System.out.println("出bug了 ,出现了除01A,01B之外的其他值");
break;
}
}
if(a == myArray.size() ){
System.out.println(a+"个handwork:01A");
counta++;
}else if(b == myArray.size()){
System.out.println(b+"个handwork:01B");
countb++;
}else{
System.out.println("出bug了 ,返回字段值不全是01A,或者01B");
break;
}
//driver.close();
}
System.out.println("共打开页面"+c+"次");
System.out.println("其中出现01A:"+counta+"次");
System.out.println("其中出现01B:"+countb+"次");
}
}
public static void main(String[] args){
String url = "/getCpcDatas?keyword=iphone&positionID=100000001&inputCpcNumbers=13";
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to(url);
Window window = driver.manage().window();
window.maximize();
driver.navigate().refresh();
String html = driver.getPageSource();
String s1 = "
apsAdboardObj.aps_adboard_jsonpCallback(";";
String s2 = ",'cpc',100000001)
String str = "'"+html.substring(s1.length(), html.length()-s2.length()-1)+"'";
String jsCode = "alert(JSON.parse("+str+")[0].apsClickUrl)";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(jsCode);
}