gpt4 book ai didi

java - 与网页自动交互

转载 作者:行者123 更新时间:2023-12-01 15:57:44 24 4
gpt4 key购买 nike

我需要从 web page 检索信息只接受表单作为输入。

我通常使用 HttpClient 来检索信息并解析 html,但此页面采用 Javascript 而不是显式 url 作为参数,并再次将输入打印为 javascript。

我尝试过 HTMLUnit,但速度慢得令人痛苦,尤其是在 Android 上。此外,我似乎无法使其正常工作,因为我无法在填写表单后检索 html,可能是由于该页面是 javascrpit(请参阅附加代码)。

如何从中检索信息 web page在安卓中?

干杯:D

package com.htmlUnitTest;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class HtmlUnitTest {

/**
* @param args
*/
public static void main(String[] args) {

try {

submittingForm();


} catch (Exception e) {

System.out.println("Something bad happened");

e.printStackTrace();
}



}



static public void submittingForm() throws Exception {
final WebClient webClient = new WebClient();

// Get the first page
final HtmlPage page1 = webClient.getPage(
"http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione?lang=IT");

System.out.println(page1.getForms());

// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getForms().get(0);

final HtmlSubmitInput button = form.getInputByValue(" Cerca ");
final HtmlTextInput textField = form.getInputByName("stazione");

// Change the value of the text field
textField.setValueAttribute("Milano Centrale");

// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();

System.out.println(page2.getAnchors());

webClient.closeAllWindows();
}


}

最佳答案

您需要发布到/viaggiatreno/mobile/stazione,解析答案,然后如果您想深入了解,则再次发布。有关在此处发帖的更多信息:google.com/search?q=webClient+post

关于java - 与网页自动交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4768295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com