如何使用 HtmlUnit 搜索 YouTube
我想知道是否可以使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 HtmlUnit 专家,但有一个解决方法。您可以将自己的按钮添加到表单并使用它来提交表单。
这是带有注释的代码示例:
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:
HtmlUnit 还可以,但我更喜欢 Watir 或 Selenium 用于网络自动化。
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"