android web scrapping动态内容jsoup

时间:2017-07-16 18:56:01

标签: android web-scraping jsoup

我正在开发一个Android应用程序,它应该跟踪来自以色列邮局网站的货物。因为他们没有任何api,我试图通过设置特定链接和使用jsoup解析html来操纵网站,但它无法正常工作,因为跟踪信息是动态加载的。我尝试将jsoup与selenium结合但我不确定它是否可以在android环境中实现,因为我得到构建错误是否有人有任何技术或方法来执行此任务?我非常感激。trying to capture the info in blue box

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以从此网址获取数据: http://www.israelpost.co.il/itemtrace.nsf/trackandtraceNOHEJSON?openagent&lang=EN&itemcode=RR123445677IL

检查此Java代码:

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class IsraeliPost {
    public static void main(String[] args) {

        String URL = "http://www.israelpost.co.il/itemtrace.nsf/trackandtraceNOHEJSON?openagent&lang=EN&itemcode=";
        String itemNumber = "RR123445677IL";

        try {
            Connection.Response response = Jsoup.connect(URL + itemNumber)
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .execute();

            String jsonResponse = response.body();
            ItemData itemData = new Gson().fromJson(jsonResponse, ItemData.class);
            String itemCodeInfo = itemData.getItemcodeinfo();

            Document document = Jsoup.parse(itemCodeInfo);

            Elements table = document.select("table").select("tbody");

            for (Element raw : table) {
                Elements tds = raw.select("td");
                for (Element td : tds) {
                    System.out.println(td.text());

                }
            }

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

    public class ItemData {

        @SerializedName("itemcodeinfo")
        private String mItemcodeinfo;

        public String getItemcodeinfo() {
            return mItemcodeinfo;
        }
    }
}

<强>输出:

  

日期邮政单位城市描述30/01/2015 Shikun Memshalti Nahariya   交付授权的收件人28/01/2015 Shikun   Memshalti Nahariya抵达邮政单位送货   addressee 27/01/2015 Jaffo Tel Aviv Yaffo收到邮寄信息   转发处理