模拟登陆手机版新浪微博

分析登陆步骤(http://login.weibo.cn

首页

点击登录
登陆界面
注意:登陆链接不是http://login.weibo.cn,而是http://login.weibo.cn/login/?ns=1&revalid=2&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%CE%A2%B2%A9&vt=,观察发现该链接为固定链接

观察登陆所需参数(采用Firefox firebug)
请求体信息
发现字段backURL,password_****,vk为变化字段,需要获取

请求头信息
发现Cookie字段,需要获取

字段获取
这里写图片描述
登陆界面源码可以获取相关字段
登陆界面响应头信息获取cookie

登陆之后再次跳转
这里写图片描述
获取cookie和location(跳转链接)

完整代码


import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


public class SinaCrawler {
    public static void main(String[] args){
        CloseableHttpClient client = HttpClients.createDefault();

        CloseableHttpResponse response = null;
        try{
            //访问http://login.weibo.cn会跳转,直接访问点击登录按钮后的链接
            HttpGet get = new HttpGet("http://login.weibo.cn/login/?ns=1&revalid=2&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%CE%A2%B2%A9&vt=");
            response = client.execute(get);
            String cookie = null;
            for(Header header:response.getHeaders("Set-Cookie")){
                 cookie = header.toString().split(":")[1].trim();
            }
System.out.println(cookie);
            HttpEntity entity = response.getEntity();
            String html = EntityUtils.toString(entity);              //获取网页源码
            EntityUtils.consume(entity);

            //采用jsoup解析,获取password_字段,backURL字段,vk字段
            Document doc = Jsoup.parse(html);
            Element form = (Element) doc.select("form").first(); 
            String action = form.attr("action");
System.out.println(action);
            Document formDoc = Jsoup.parse(form.html());

            //获取backURL
            Element a = formDoc.select("input[name=backURL]").first(); 
            String backURL = a.attr("value");
//System.out.println(backURL);

            //获取password_
            Element passwordE = formDoc.select("input[type=password]").first();
            String password = passwordE.attr("name");
//System.out.println(password);

            //获取vk
            Element vkE = formDoc.select("input[name=vk]").first();
            String vk = vkE.attr("value");
//System.out.println(vk);

            HttpPost post = new HttpPost("http://login.weibo.cn/login/"+action);
System.out.println("http://login.weibo.cn/login/"+action);          

            post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:39.0) Gecko/20100101 Firefox/39.0");
            post.setHeader("Referer", "http://login.weibo.cn/login/?ns=1&revalid=2&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%CE%A2%B2%A9&vt");
            post.setHeader("Host", "login.weibo.cn");
            post.setHeader("Cookie", cookie);

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
            nvps.add(new BasicNameValuePair("backTitle", "微博"));  
            nvps.add(new BasicNameValuePair("backURL", backURL));  
            nvps.add(new BasicNameValuePair("mobile", "*****"));  
            nvps.add(new BasicNameValuePair(password, "*****"));  
            nvps.add(new BasicNameValuePair("remember", "on"));
            nvps.add(new BasicNameValuePair("submit", "登录"));
            nvps.add(new BasicNameValuePair("tryCount", ""));
            nvps.add(new BasicNameValuePair("vk", vk));

            if (nvps != null) {  
                post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));  
            }  

            response = client.execute(post);  

            StringBuffer cookieLogin = new StringBuffer();
            for(Header header:response.getHeaders("Set-Cookie")){
//System.out.println(header);
                cookieLogin.append(header.toString().split(":")[1]);
            }

            for(Header header:response.getHeaders("Set-Cookie")){
//System.out.println(header);
                cookieLogin.append(header.toString().split(":")[1]);
            }


System.out.println(cookieLogin.toString());

            String location = null; 
            for(Header header:response.getHeaders("Location")){
                location = header.toString().split(": ")[1];
            }
System.out.println(location);

            HttpGet get3 = new HttpGet(location);
            get3.setHeader("Cookie",cookieLogin.toString());
            response = client.execute(get3);

            HttpEntity entity2 = response.getEntity();  
System.out.println("contentLength:"+entity2.getContentLength());            
            String content = EntityUtils.toString(entity2);  
System.out.println("content:"+content);         
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(response!=null){
                try{
                    response.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }


    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Python代码示例,用于模拟登陆新浪微博。请注意,这只是一个示例,具体的实现方法可能因为新浪微博的更新而有所不同。 ```python import requests from lxml import etree # 定义登录函数 def login(username, password): # 创建一个Session对象,用于保持登录状态 session = requests.Session() # 访问登录页面,获取一些必要的参数 url = 'https://login.sina.com.cn/signup/signin.php' response = session.get(url) html = etree.HTML(response.text) vk = html.xpath('//input[@name="vk"]/@value')[0] action = html.xpath('//form[@id="SinaLoginform"]/@action')[0] # 构造登录请求 data = { 'username': username, 'password': password, 'vk': vk, 'returntype': 'META', 'encoding': 'UTF-8', 'entry': 'sso', 'gateway': '1', 'from': 'null', 'savestate': '30', 'userticket': '0', 'pagerefer': '', 'wsseretry': 'servertime_error', 'pcid': '', 'door': '', 'appkey': '1', 's': '1', 'vsnf': '1', 'su': '', 'service': 'sso', 'servertime': '', 'nonce': '', 'pwencode': 'rsa2', 'rsakv': '', 'sp': '', 'sr': '', } response = session.post(action, data=data, allow_redirects=False) # 获取重定向链接,访问该链接即可完成登录 location = response.headers.get('Location') session.get(location) return session # 使用示例 if __name__ == '__main__': username = 'your_username' password = 'your_password' session = login(username, password) # 使用session对象访问需要登录后才能访问的页面 response = session.get('https://weibo.com/') print(response.text) ``` 请将 `your_username` 和 `your_password` 替换为您自己的用户名和密码。此外,需要安装 `requests` 和 `lxml` 两个库,可以使用 `pip` 命令进行安装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值