前言:
今天同学们对“js获取input的name”大体比较重视,大家都想要分析一些“js获取input的name”的相关知识。那么小编在网上汇集了一些对于“js获取input的name””的相关知识,希望大家能喜欢,咱们快快来学习一下吧!一般情况下,为了保证安全性,网站会定期更新登录的detail,例如修改参数名、更新加密(散列)算法等。所以模拟登录的代码定期肯定会失效,但是如果网站没有进行大的更新的话,稍微改一改还是能用的。另外,碰到验证码的情况就更难办了,虽然程序可以一定程度地识别验证码字符,但目前很难找到简单的可以通用的验证码识别程序。
很多朋友有模拟登录新浪微博抓取数据的需求,其实对于一般的微博数据获取,如用户信息、微博内容等,使用微博开放平台API是更明智的选择:速度更快,而且节省许多网页处理的功夫。对于API没有开放的数据,我们再采用模拟登录的方法。不多说了,我们直接上代码:(相关JAR文件地址:)
package cn.com.leepeng.test;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import net.sf.json.JSONObject;
import weibo4j.util.CommonHttpProtocolRequestUtil;
/**
* 微博模拟登录获取AccessToken工具
*
* @author Alex.Lee
* @mail lp@zving.com
* @date 3rd Apr ,2018
*
*/
public class Weibo {
private static final String AUTH_URL = "{0}&redirect_uri={1}";
private static final String AUTH_TOKEN = "";
private String clientID;
private String redirectUri;
private String username;
private String password;
private String clientSecret;
private String code;
public Weibo() {
super();
}
public Weibo(String clientID, String redirectUri, String username, String password, String clientSecret) {
super();
this.clientID = clientID;
this.redirectUri = redirectUri;
this.username = username;
this.password = password;
this.clientSecret = clientSecret;
}
public String getClientID() {
return clientID;
}
public void setClientID(String clientID) {
this.clientID = clientID;
}
public String getRedirectUri() {
return redirectUri;
}
public void setRedirectUri(String redirectUri) {
this.redirectUri = redirectUri;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return JSONObject.fromBean(this).toString();
}
public static class AccessTokenData {
String access_token;
String remind_in;
String expires_in;
String uid;
String isRealName;
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public void setRemind_in(String remind_in) {
this.remind_in = remind_in;
}
public void setExpires_in(String expires_in) {
this.expires_in = expires_in;
}
public void setUid(String uid) {
this.uid = uid;
}
public void setIsRealName(String isRealName) {
this.isRealName = isRealName;
}
public String getAccess_token() {
return access_token;
}
public String getRemind_in() {
return remind_in;
}
public String getExpires_in() {
return expires_in;
}
public String getUid() {
return uid;
}
public String getIsRealName() {
return isRealName;
}
@Override
public String toString() {
return JSONObject.fromBean(this).toString();
}
}
public AccessTokenData getAccessTokenData() throws Exception {
WebClient webClient = null;
String authUrl = null;
Weibo.AccessTokenData accessTokenData = null;
try {
webClient = getWebClient();
authUrl = MessageFormat.format(AUTH_URL, clientID, redirectUri);
HtmlPage page = (HtmlPage) webClient.getPage(authUrl);
HtmlForm form = page.getForms().get(0);
form.getInputByName("userId").setValueAttribute(this.username);
form.getInputByName("passwd").setValueAttribute(this.password);
page = (HtmlPage) form.getOneHtmlElementByAttribute("a", "action-type", "submit").click();// 登录进入
webClient.waitForBackgroundJavaScript(1000);// 等待1秒
String baseURI = page.getBaseURI();
if (baseURI != null && baseURI.indexOf("code") > 0) {
String code = baseURI.substring(baseURI.indexOf("code") + 5);
this.setCode(code);
Map<String, String> params = new HashMap<>();
params.put("client_id", clientID);
params.put("client_secret", clientSecret);
params.put("grant_type", "authorization_code");
params.put("code", code);
params.put("redirect_uri", redirectUri);
accessTokenData = (Weibo.AccessTokenData) JSONObject.toBean(
JSONObject.fromString(CommonHttpProtocolRequestUtil.requestWithPost(AUTH_TOKEN, params)),
Weibo.AccessTokenData.class);
}
} catch (Exception e) {
throw e;
} finally {
webClient.close();
}
return accessTokenData;
}
public static WebClient getWebClient() {
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true); // 启动JS
webClient.getOptions().setUseInsecureSSL(true);// 忽略ssl认证
webClient.getOptions().setCssEnabled(false);// 禁用Css,可避免自动二次请求CSS进行渲染
webClient.getOptions().setThrowExceptionOnScriptError(false);// 运行错误时,不抛出异常
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 设置Ajax异步
return webClient;
}
public static void main(String[] args) throws Exception {
Weibo weibo = new Weibo("xxxx", "", "xxxxxx",
"xxxxx", "xxxxxxx");
AccessTokenData accessTokenData = weibo.getAccessTokenData();
System.out.println(accessTokenData);
}
}
标签: #js获取input的name #accesstoken获取