2016-04-10 2 views
1

Я пытаюсь написать немного бота, который должен получить доступ к этому сайту http://lsa.colorado.edu/cgi-bin/LSA-pairwise.html, введите текст в textarea и получить полученную страницу от отправки, нажав кнопку отправки. Это для лингвистического проекта. Однако, когда я выполнить щелчок на кнопке HtmlSubmitInput возвращаемый URL, кажется, неправильный формат, как IncorrectnessListenerImpl уведомляет меня:HtmlUnit HtmlSubmitInput.click() приводит к «неправильному URL», скорректированному на «cgi-bin», который затем приводит к UnknownHostException

Apr 10, 2016 2:38:35 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 
WARNUNG: Incorrect URL "http:/cgi-bin/LSA-pairwise-x.html" has been corrected 

URL-адрес должен быть

http://lsa.colorado.edu/cgi-bin/LSA-pairwise-x.html

Это то приводит к следующей стопке (сокращенно по длине):

Exception in thread "main" java.lang.RuntimeException: java.net.UnknownHostException: cgi-bin: unknown error 
    at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2078) 
    at com.gargoylesoftware.htmlunit.html.HtmlForm.submit(HtmlForm.java:141) 
    at com.gargoylesoftware.htmlunit.html.HtmlSubmitInput.doClickStateUpdate(HtmlSubmitInput.java:90) 
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:795) 
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:742) 
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:689) 
    at LSABot.submitInput(LSABot.java:30) 
    at Start.main(Start.java:8) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 
[...] 

Я предполагаю, что HtmlUnit пытается исправить URL-адрес, но это приводит только к «cgi-bin», что, конечно, неверно. Я искал снова и снова, но не нашел ничего подходящего для моей проблемы.

Мой класс LSABot:

public class LSABot { 
    final WebClient webClient; 
    private HtmlPage mainPg, rsltPg; 
    private HtmlForm htmlForm; 
    private HtmlTextArea txtA; 
    private HtmlSubmitInput submitBt; 

    public LSABot() throws Exception { 
     this.webClient = new WebClient(BrowserVersion.CHROME); 
     this.webClient.getOptions().setJavaScriptEnabled(true); 
     this.mainPg = this.webClient.getPage("http://lsa.colorado.edu/cgi-bin/LSA-pairwise.html"); 
     this.htmlForm = this.mainPg.getForms().get(0); 
     this.txtA = this.htmlForm.getTextAreaByName("txt1"); 
     this.submitBt = this.htmlForm.getInputByValue("Submit Texts"); 
    } 

    public void submitInput(String input) { 
     this.txtA.setText(input); 
     try { 
      this.rsltPg = this.submitBt.click(); 
      this.webClient.waitForBackgroundJavaScript(30*1000); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 

ответ

0

ошибка происходит от содержания HTML формы. Атрибут action должен быть http://lsa.colorado.edu/cgi-bin/LSA-pairwise-x.html вместо http:/cgi-bin/LSA-pairwise-x.html.

Попробуйте этот код, он должен работать:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); 

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF); 

WebClient client = new WebClient(BrowserVersion.CHROME); 
client.getOptions().setJavaScriptEnabled(true); 
client.getOptions().setThrowExceptionOnScriptError(false); 
client.getOptions().setThrowExceptionOnFailingStatusCode(false); 

String url = "http://lsa.colorado.edu/cgi-bin/LSA-pairwise.html"; 
final HtmlPage page = client.getPage(url); 

HtmlForm htmlForm = page.getForms().get(0); 
HtmlTextArea txtA = htmlForm.getTextAreaByName("txt1"); 
txtA.setText("hello"); 
HtmlSubmitInput submitBt = htmlForm.getInputByValue("Submit Texts"); 

// change the form action attribute to the correct one 
htmlForm.setAttribute("action", "http://lsa.colorado.edu/cgi-bin/LSA-pairwise-x.html"); 

HtmlPage page2 = submitBt.click(); 
System.out.println(page2.asText()); 
+0

Brilliant. Спасибо! Метод HtmlForm.setAttribute() был именно тем, что мне нужно! –

Смежные вопросы