Cercando di compilare un modulo sito web utilizzando Java, ma il tag modulo è incorporato nel tag iframe


Il mio obiettivo è accedere a questo URL http://eaacorp.com/find-a-dealer e compilare un modulo utilizzando java. Per fare ciò, ho tentato di trovare tutti i tag del modulo:

import java.io.IOException;

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

public class HttpUrlConnectionExample{

    public static void main(String[] args) throws IOException{
        Document document = Jsoup.connect("http://eaacorp.com/find-a-dealer").get();
        String page = document.toString();//this is the whole page's html

        Elements formEl = document.getElementsByTag("form");
    }

}

Tuttavia formEl restituisce vuoto perché il tag form è incorporato nel http://www.eaacorp.com/dealer/searchdealer.php html nel tag iframe (frammento dell'origine della pagina):

<iframe id="blockrandom" name="iframe" src="http://www.eaacorp.com/dealer/searchdealer.php" width="100%" height="500" scrolling="auto" frameborder="1" class="wrapper"></iframe>

Quindi, c'è un modo per accedere al tag del modulo all'interno del tag iframe? Qualcosa come:

if(formEl.isEmpty()){
    //find iframe
    Elements iframeEl = document.getElementsByTag("iframe");
    System.out.println(iframeEl);
    String embedURL = iframeEl.getSrc();//DOES NOT COMPILE, getSrc() is not a method
    Document embedDoc = Jsoup.connect(embedURL).get();
}
Author: Travis Ho, 2016-10-08

2 answers

Non è necessario il proprio metodo getSrcString, soprattutto perché l'approccio sottostringa si interromperà per modifiche minime nel tag.

Usa .attr("abs:src") su un elemento con l'attributo src (confronta: https://jsoup.org/cookbook/extracting-data/working-with-urls )

Codice di esempio

Document document = Jsoup.connect("http://eaacorp.com/find-a-dealer").get();
Element iframeEl = document.select("iframe").first();
String embedURL = iframeEl.attr("abs:src");
Document embedDoc = Jsoup.connect(embedURL).get();

System.out.println(embedDoc.select("form").first());

Uscita troncata

<form action="findit.php" method="post" name="dlrsrchfrm" target="_blank"> 
    <div style="padding: 15px;">
    [...]
</form> 
 0
Author: Frederic Klein, 2016-10-10 06:27:16

Ho scoperto che potresti effettivamente creare il tuo metodo che può ottenere l'url src usando sottostringhe e quindi usare quella stringa per ottenere una connessione al documento:

public static String getSrcString(String html){
    String construct = "";
    for (int i = 0; i < html.length() - 5;i++){
        if (html.substring(i, i + 5).equals("src=\"")){
            i += 5;
            while(!html.substring(i, i + 1).equals("\"")){
                construct += html.substring(i, i + 1);
                i++;
            }
        }
    }
    return construct;
}

E poi nel principale:

String embedURL = getSrcString(iframeEl.toString());
Document embedDoc = Jsoup.connect(embedURL).get();
 0
Author: Travis Ho, 2016-10-08 20:11:59