我写了这段代码:
import com.gargoylesoftware.htmlunit.BrowserVersion;
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.HtmlTextArea;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author user
*/
public class YouTubeComment {
public static void main(String[] args) {
boolean f = YouTubeLogin.login();
try {
if (f) {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setRedirectEnabled(true);
webClient.setJavaScriptEnabled(false);
HtmlPage firstPage = webClient.getPage("http://www.youtube.com/watch?v=kqDacBDoVM4&feature=related");
List<HtmlForm> forms = new ArrayList();
forms = (List<HtmlForm>) firstPage.getForms();
HtmlForm form = firstPage.getForms().get(1);
HtmlTextArea commentArea = (HtmlTextArea)form.getTextAreaByName("comment");
commentArea.setText("good");
HtmlSubmitInput submitButton =(HtmlSubmitInput)form.getInputByName("");
HtmlPage pageAfterPost = (HtmlPage) submitButton.click();
} else {
System.out.println("Sorry..! Login is not successful");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
此应用程序可以使用用户名和密码登录 YouTube 帐户。
我想写一些代码在成功登录后发表评论。
请帮忙。