如何在Android中使用高javascript的网站进行检索?

时间:2015-12-12 15:33:08

标签: javascript android jsoup

http://www.biletix.com/search/TURKIYE/en#!subcat_interval:12/12/15TO19/12/15

我想从这个网站获取数据。当我使用jsoup时,由于javascript,它无法执行。尽管我付出了很多努力,仍然无法管理。

enter image description here

如你所见,我只想得到名字和网址。然后我可以去那个网址并获得开始时间和地点。

我不想使用无头浏览器。你知道其他选择吗?

1 个答案:

答案 0 :(得分:1)

有时基于javascript和json的网页比普通的html网页更容易被抓取。

如果仔细检查网络流量(例如,使用浏览器开发人员工具),您将意识到该页面正在发出一个GET请求,该请求返回包含您需要的所有数据的json字符串。您将能够使用任何json库解析该json。

网址是:

http://www.biletix.com/solr/en/select/?start=0&rows=100&fq=end%3A[2015-12-12T00%3A00%3A00Z%20TO%202015-12-19T00%3A00%3A00Z%2B1DAY]&sort=vote%20desc,start%20asc&&wt=json

您可以使用与生成问题中的网址类似的方式生成此网址。

你得到的json片段是:

....
 "id":"SZ683",
 "venuecount":"1",
 "category":"ART",
 "start":"2015-12-12T18:30:00Z",
 "subcategory":"tiyatro$ART",
 "name":"The Last Couple to Meet Online",
 "venuecode":"BT",
.....

在那里,您可以看到使用id字段(SZ683)轻松生成名称和网址,例如:http://www.biletix.com/etkinlik/SZ683/TURKIYE/en

-------编辑-------

获取json数据比我最初的想法更困难。服务器需要cookie才能返回正确的数据,因此我们需要:

  • 要进行第一次GET,请获取cookie并进行第二次GET以获取json数据。使用Jsoup很容易。
  • 然后我们将使用org.json解析响应。

这是一个有效的例子:

//Only as example please DON'T use in production code without error control and more robust parsing
//note the smaller change in server will break this code!!
public static void main(String[] args) throws IOException {
    //We do a initial GET to retrieve the cookie
    Document doc = Jsoup.connect("http://www.biletix.com/").get();
    Element body = doc.head();
    //needs error control 
    String script = body.select("script").get(0).html();

    //Not the more robust way of doing it ...
    Pattern p = Pattern.compile("document\\.cookie\\s*=\\s*'(\\w+)=(.*?);");
    Matcher m = p.matcher(script);
    m.find();
    String cookieName = m.group(1);
    String cookieValue = m.group(2);

    //I'm supposing url is already built
    //removing url last part (json.wrf=jsonp1450136314484) result will be parsed more easily 
    String url = "http://www.biletix.com/solr/tr/select/?start=0&rows=100&q=subcategory:tiyatro$ART&qt=standard&fq=region:%22ISTANBUL%22&fq=end%3A%5B2015-12-15T00%3A00%3A00Z%20TO%202017-12-15T00%3A00%3A00Z%2B1DAY%5D&sort=start%20asc&&wt=json";

    Document document = Jsoup.connect(url)
            .cookie(cookieName, cookieValue) //introducing the cookie we will get the corect results
            .get();
    String bodyText = document.body().text();

    //We parse the json and extract the data
    JSONObject jsonObject = new JSONObject(bodyText);
    JSONArray jsonArray = jsonObject.getJSONObject("response").getJSONArray("docs");
    for (Object object : jsonArray) {
        JSONObject item = (JSONObject) object;
        System.out.println("name = " + item.getString("name"));
        System.out.println("link = " + "http://www.biletix.com/etkinlik/" + item.getString("id") + "/TURKIYE/en");
        //similarly you can fetch more info ...
        System.out.println();
    }
}

我跳过了URL生成,因为我想你知道如何生成它。

我希望所有的解释都清楚,英语不是我的第一语言所以我很难解释自己。