`

通过访问地址抓取网页数据

阅读更多
public class Test2 {

public static void main(String[] args) throws HttpException, IOException {

// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//获取xml字符串的地址
String url2="http://oa.shunde.gov.cn/egp/OAXSERVICE?ACTION=CommonChannel.CommonAction.getSelInfo&token=123";
       //String url2="http://item.yixun.com/item-386275.html";
System.out.println("url------------>>" + url2);
                 //get方法实例
GetMethod getMethod = new GetMethod(url2);
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

new DefaultHttpMethodRetryHandler());
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getMethod.getResponseBody();

String ISOname = "";

ISOname = new String(responseBody, "utf-8");

// 处理内容
System.out.println("ISOname----------------------->>"
+ new String(ISOname));
             //解析内容
//readXml(new String(ISOname));

} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
}
    /**
     * 解析抓取的xml字符串
     * @param strXml
     */
public static void readXml(String strXml) {

System.out.println("111111111111111111111111");
Document document;

try {
document = DocumentHelper.parseText(strXml);

Element users = document.getRootElement();

for (Iterator iter = users.elementIterator(); iter.hasNext();) {

Element element = (Element) iter.next();

Attribute id = element.attribute("id");

Attribute name = element.attribute("name");

Attribute orgId = element.attribute("orgId");

Attribute parentId = element.attribute("parentId");

Attribute frzg = element.attribute("frzg");

Attribute addr = element.attribute("orgId");

System.out.println("id---------------------<" + id.getText());

System.out.println("name---------------------<"
+ name.getText());

System.out.println("orgId---------------------<"
+ orgId.getText());

System.out.println("parentId---------------------<"
+ parentId.getText());

System.out.println("frzg---------------------<"
+ frzg.getText());

System.out.println("addr---------------------<"
+ addr.getText());
}

} catch (DocumentException e) {
e.printStackTrace();
}
}

附件为jar包
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics