有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用HtmlUnit搜索YouTube

我想知道YouTube是否可以用HtmlUnit搜索。我开始编写代码,如下所示:

import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class HtmlUnitExampleTestBase {
    private static final String YOUTUBE = "http://www.youtube.com";
    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        WebClient webClient = new WebClient();
        webClient.setThrowExceptionOnScriptError(false);

        //This is equivalent to typing youtube.com to the adress bar of browser
        HtmlPage currentPage = webClient.getPage("http://www.youtube.com");

        //Get form where submit button is located
        HtmlForm searchForm = (HtmlForm) currentPage.getElementById("masthead-search");
        //Printing result form
        System.out.println(searchForm.asText());
        final List<HtmlAnchor> listLinks = (List<HtmlAnchor>) newPage.getByXPath("//a[@class='ux-thumb-wrap result-item-thumb']");
        for (int i=0; i<listLinks.size(); i++){
            System.out.println(YOUTUBE + listLinks.get(i).getAttribute("href"));
        }
    }   
}

现在我不知道如何在搜索字段中键入一些文本并按下搜索按钮

我看过关于HtmlUnit的教程,但我遇到了一个问题,因为他们使用了一个名为:getElementByName的方法,但YouTube上的搜索按钮没有名字,只有id。有人能帮我吗

编辑:我在代码上面编辑了代码,现在我从第一页获得了youtube链接。但在此之前,我需要按上传日期排序,然后抓取链接。有人能帮我分类吗


共 (2) 个答案

  1. # 1 楼答案

    HtmlUnit还可以,但我更喜欢WatirSelenium实现web自动化

    HtmlUnit的一个缺点是缺乏选择器方法,无法以类似jQuery的方式获取DOM元素。查看css选择器项目,该项目将添加到HtmlUnit中,帮助您轻松完成所需任务。在Gooder Code有一个介绍

    一旦你开始工作,YouTube搜索表单的选择器将是“.search term”,提交按钮的选择器将是“.search button”

  2. # 2 楼答案

    我不是专家,但有一个解决办法。您可以将自己的按钮添加到表单中,并使用它提交表单

    下面是一个带有注释的代码示例:

    import java.io.IOException;
    import java.net.MalformedURLException;
    
    import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlButton;
    import com.gargoylesoftware.htmlunit.html.HtmlForm;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
    
    public class HtmlUnitExampleTestBase {
       public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
          WebClient webClient = new WebClient();
          webClient.setThrowExceptionOnScriptError(false);
    
          // This is equivalent to typing youtube.com to the adress bar of browser
          HtmlPage currentPage = webClient.getPage("http://www.youtube.com");
    
          // Get form where submit button is located
          HtmlForm searchForm = (HtmlForm) currentPage.getElementById("masthead-search");
    
          // Get the input field.
          HtmlTextInput searchInput = (HtmlTextInput) currentPage.getElementById("masthead-search-term");
          // Insert the search term.
          searchInput.setText("Nyan Cat");
    
          // Workaround: create a 'fake' button and add it to the form.
          HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
          submitButton.setAttribute("type", "submit");
          searchForm.appendChild(submitButton);
    
          // Workaround: use the reference to the button to submit the form. 
          HtmlPage newPage = submitButton.click();
    
          System.out.println(newPage.asText());
       }
    }