zoukankan      html  css  js  c++  java
  • 使用java代码批量删除新浪微博

    首先开骂,新浪微博@#@!,不经我同意就转发各种微博,制造垃圾,还不提供微博批量删除功能,摆明了的流氓行为,可耻可恨,遭人唾弃!

    SSLClient.java

    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.conn.scheme.Scheme;
    import org.apache.http.conn.scheme.SchemeRegistry;
    import org.apache.http.conn.ssl.SSLSocketFactory;
    import org.apache.http.impl.client.DefaultHttpClient;
    
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    public class SSLClient extends DefaultHttpClient {
    
        public SSLClient() throws Exception {
            super();
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
    
                @Override
                public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
                }
    
                @Override
                public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
                }
    
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = this.getConnectionManager();
            SchemeRegistry sr = ccm.getSchemeRegistry();
            sr.register(new Scheme("https", 443, ssf));
        }
    }

    WeiboTest.java

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.message.BasicNameValuePair;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    public class WeiboTest {
    
        public static final String url = "https://weibo.com/aj/mblog/del?ajwvr=6";
        public static final String charset = "utf-8";
    
        public static void main(String[] args) throws Exception {
            /**
             * sina_weibo.txt由微博页面F12获得格式是
             * <div node-type="feed_list">
             *     <div action-type="feed_list_item"></div>
             *     <div action-type="feed_list_item"></div>
             *     <div action-type="feed_list_item"></div>
             *     <div action-type="feed_list_item"></div>
             *     <div action-type="feed_list_item"></div>
             *     ...
             * </div>
             */
            File file = new File("C:\Users\Nihaorz\Desktop\sina_weibo.txt");
            BufferedReader br = new BufferedReader(new FileReader(file));
            StringBuffer sb = new StringBuffer();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str).append("
    ");
            }
            System.out.println(sb);
            br.close();
    
            Document doc = Jsoup.parseBodyFragment(sb.toString());
            Element body = doc.body();
            Elements elements = body.select("div[action-type='feed_list_item']");
            Iterator<Element> it = elements.iterator();
            HttpPost httpPost = new HttpPost(url);
            addHeader(httpPost);
            while (it.hasNext()) {
                Element element = it.next();
                String mid = element.attr("mid");
                System.out.println(mid);
                execute(httpPost, mid);
            }
        }
    
        private static void execute(HttpPost httpPost, String mid) throws Exception {
            HttpClient httpClient = new SSLClient();
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Map<String, String> map = new HashMap<String, String>();
            map.put("mid", mid);
            Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> elem = iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
                httpPost.setEntity(entity);
            }
            HttpResponse resp = httpClient.execute(httpPost);
            System.out.println(resp);
        }
    
        private static void addHeader(HttpPost httpPost) {
            httpPost.addHeader("Accept", "*/*");
            httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
            httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpPost.addHeader("Connection", "keep-alive");
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            /**
             * 设置自己的cookie
             */
            httpPost.addHeader("Cookie", "***");
            httpPost.addHeader("Host", "weibo.com");
            httpPost.addHeader("Origin", "https://weibo.com");
            httpPost.addHeader("Referer", "https://weibo.com/1864722372/profile?rank=1&is_all=1");
            httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36");
            httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
        }
    
    }
  • 相关阅读:
    A方法调用B方法,JVM是怎么一步一步调用的
    java ImmutableMap使用
    使用 Spring 配置动态数据源实现读写分离
    spring-boot的三种启动方式
    使用 Spring 配置动态数据源实现读写分离
    Java改变生成随机数的平均值(改变生成随机数的概率)
    微信抢红包算法实现(JAVA)
    Redis分布式锁的实现原理
    Redis和队列
    springboot自定义配置文件
  • 原文地址:https://www.cnblogs.com/nihaorz/p/8295658.html
Copyright © 2011-2022 走看看