手机版

Selenium解析Json格式数据

时间:2025-05-10   来源:未知    
字号:

Selenium解析Json格式数据

添加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 = "<html xmlns=\"http://www.77cn.com.cn/1999/xhtml\"><head><link title=\"将较长的行自动换行\" href=\"resource://gre-resources/plaintext.css\" type=\"text/css\" rel=\"alternate stylesheet\" /></head><body><pre>";
String strLast = "</pre></body></html>";
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 http://www.77cn.com.cnsuning.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"

Selenium解析Json格式数据

;

//如果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 = "<html xmlns=\"http://www.77cn.com.cn/1999/xhtml\"><head></head><body><pre>{\"sugGoods\":[";
String s2 = "]}</pre></body></html>";
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<myArray.size();i++){
//JSONObject o = myArray.getJSONObject(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&qu …… 此处隐藏:3101字,全部文档内容请下载后查看。喜欢就下载吧 ……

Selenium解析Json格式数据.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
    ×
    二维码
    × 游客快捷下载通道(下载后可以自由复制和排版)
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
    × 常见问题(客服时间:周一到周五 9:30-18:00)