如何使用 HtmlUnit 搜索 YouTube

发布于 2024-11-25 11:33:44 字数 1755 浏览 2 评论 0原文

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

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 链接。但在此之前,我需要按上传日期排序,然后获取链接。有人可以帮我排序吗?

I wonder if YouTube could be searched with HtmlUnit. I started to write code, here it is:

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"));
        }
    }   
}

Now I don't know how to type some text into a search field and press Search button.

I saw tutorials about HtmlUnit but I'm having a problem because they use a method named: getElementByName but the search button on YouTube doesn't have a name, just an id. Could someone help me?

EDIT: I edited code above code and now I am getting youtube links from first page. But before that I need to sort by upload date and then to grab links. Can someone help me to do sorting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2024-12-02 11:33:44

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

这是带有注释的代码示例:

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());
   }
}

I'm no HtmlUnit expert, but there is a workaround. You can add your own button to the form and use it to submit the form.

Here's a code sample with comments:

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());
   }
}
南风起 2024-12-02 11:33:44

HtmlUnit 还可以,但我更喜欢 WatirSelenium 用于网络自动化。

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

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

HtmlUnit is OK, but I vastly prefer Watir or Selenium for web automation.

One of HtmlUnit's weaknesses is its lack of selector methods for getting at DOM elements in a jQuery-like way. Check out the css-selector project, which will add on to HtmlUnit to help you do what you need very easily. There's an intro at Gooder Code.

Once you get that working, the selector for the YouTube search form would be ".search-term" and the submit button's selector would be ".search-button"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文