简体   繁体   English

使用jsoup从新页面获取链接?

[英]Get link from new page using jsoup?

I'm trying to get prise information from a website. 我正在尝试从网站获取奖品信息。 First I must fill in a form then I want to press the submit button so I get the new page then I want to take a link from an id in the new page, but I do not know how to do that. 首先,我必须填写一个表单,然后单击提交按钮,以获取新页面,然后从新页面的ID中获取链接,但是我不知道该怎么做。

Here is my code: 这是我的代码:

package com.sigustgebran.appsl;

import android.os.Bundle;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainAct extends Activity {


TextView display;
String anerror = "error";
// url
    static final String SL_URL = "http://reseplanerare.sl.se/bin/query.exe/sn?OK#focus";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // set layout view
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_main);
    display = (TextView) findViewById(R.id.tvDisplay);
    // process
    try {
        display.setText(gethref());
    } catch (Exception ex) {
        display.setText(anerror);
    }


}



protected String gethref() throws Exception {


    Document doc = Jsoup.connect(SL_URL)
            .userAgent("Mozilla/5.0")
            .data("existCustomerProfileFlag", "yes")
            .data("REQcustomerProfileFlag", "1") 
            .data("queryPageDisplayed", "yes")
            .data("ignoreTypeCheck", "yes")
            .data("HWAI=JS!ajax", "yes")
            .data("HWAI=JS!js", "yes")
            .data("SetGlobalOptionGO_displayDataInTable", "yes")
            .data("advancedSettingsChanged", "yes")
            .data("REQ0JourneyStopsS0A", "255")
            .data("ignoreTypeCheck", "yes")
            .data("REQ0JourneyStopsS0G", "rosenmalm")
            .data("REQ0JourneyStopsS0ID", "A=1@O=Carin Bååts gata (Södertälje)@X=17683166@Y=59194956@U=74@L=300107648@B=1@p=1369809363@")
            .data("REQ0JourneyStopsZ0A", "255")
            .data("REQ0JourneyStopsZ0G", "nacka trafikplats")
            .data("REQ0JourneyStopsZ0ID", "A=1@O=ABB@X=17952752@Y=59336868@U=74@L=300109110@B=1@p=1369809363@")
            .data("REQ0HafasUnsharpSearch", "1")
            .data("existUnsharpSearch", "yes")
            .data("REQ0HafasChangeTime", "0")
            .data("REQ0HafasSearchForw", "1")
            .data("REQ0HafasSearchForw", "0")
            .data("REQ0JourneyDate", "+1")
            .data("REQ0JourneyTime", "21:20")
            .data("start", "Sök resa")
            .data("REQ0JourneyStops1A", "1")
            .data("REQ0JourneyStops1G", "")
            .data("REQ0JourneyStopover1", "")
            .data("REQ0JourneyProduct_prod_1", "1")
            .data("REQ0JourneyProduct_prod_0", "1")
            .data("REQ0JourneyProduct_prod_2", "1")
            .data("REQ0JourneyProduct_prod_3", "1")
            .data("REQ0JourneyProduct_prod_6", "1")
            .data("REQ0JourneyProduct_prod_7", "1")
            .data("REQ0JourneyProduct_prod_5", "1")
            .data("lineFilterRestrict", "2") //????
            .data("lineFilterExclude", "1")//?????
            .data("REQ0HafasOpFilter_value", "")
            .data("REQ0JourneyDep_Foot_maxDist", "2000")
            .data("REQ0HafasNoOfChanges", "1000:1")
            .data("REQ0HafasChangeMode", "1")
            .data("REQ0HafasChangeTime", "0")
            .data("REQ0HafasChangeMode", "2")
            .data("REQ0HafasChangeSupplement", "0:0")
            .data("REQ0HafasChangeMode", "4")
            .data("REQ0HafasChangePercent", "70")
            .data("start=1&SetGlobalOptionGO_displayAdvSearch=no&OK", "Sök resa") 
            .post();


    Elements test = doc.select("div#hideLinkFaresC1-0 "); // <--- I want the href link from this id     


    return test.text();
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} }

If test is a link element then you can use .attr("href") to get the link - but it looks like it's an element with a link subelement. 如果test是一个链接元素,则可以使用.attr(“ href”)获取链接-但看起来它是一个具有链接子元素的元素。

Try something like 尝试类似

return  test.select("a").first().attr("href");

which will return the link for the first hyperlink subelement of test. 这将返回测试的第一个超链接子元素的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM