我发出了get请求并将响应存储在字符串response
中:
HttpClient client = new DefaultHttpClient();
String getURL = "some_url_with_param_values";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
String response = EntityUtils.toString(resEntityGet);
但我只对具有类名<div>
的{{1}}感兴趣。所以,我这样做了:
<div class="product-data">
不幸的是,它没有用。任何帮助将不胜感激。
<小时/> 我的响应字符串基本上是一个html页面。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
InputSource is;
builder = factory.newDocumentBuilder();
is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("product-data"); //I even tried: (div class="product-data)
String test = list.item(0).getNodeValue(); //Just to test it
答案 0 :(得分:3)
我认为你应该尝试使用getElementsByClassName('product-data')
如果这不起作用,您可以随时查看Jsoup,它提供了一个库,可以轻松地从网页中提取Html元素
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url.toURI());
HttpResponse resp = client.execute(get);
String content = EntityUtils.toString(resp.getEntity());
Document doc = Jsoup.parse(content);
Elements ele = doc.select("div.classname");
此示例执行Http GET,然后使用类“classname”提取所有Div元素,然后您可以使用
执行所需的操作