java提交wordpress文章

package com.newland.cms;

import com.newland.cms.utils.HttpClientUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author leell
 * @date 2023/12/6 22:19:09
 */
public class WebstackTest {
    public static void main(String[] args) throws IOException {
//        for (int i = 1; i <= 12; i++) {
//            String contentStr = HttpClientUtil.get("https://www.naviai.cn/favorites/ai_healthcare_and_wellness/page/" + i, null);
//            Document document = Jsoup.parse(contentStr);
//            Elements aEles = document.select(".url-card .url-body a");
//            int m = 0;
//            for (Element aEle : aEles) {
//                String url = aEle.attr("href");
//                if (url.contains("/tools/")) {
//                    m++;
                    if (i == 10 && m <= 6) continue;
//                    String content = HttpClientUtil.get(url, null);
//                    String srcUrl = aEle.attr("data-url");
//                    if (srcUrl.equals("https://www.chatfast.io")) continue;
//                    publishArticle(content, srcUrl);
//                }
//            }
//        }


//        Document document = Jsoup.parse(new File("site.txt"));
//        Elements aEles = document.select("a");
//        for (Element aEle : aEles) {
//            String url = aEle.attr("href");
//            if (url.contains("/tools/")) {
//                String srcUrl = aEle.attr("data-url");
//                String content = HttpClientUtil.get(url, null);
//                publishArticle(content, srcUrl);
//            }
//        }
    }

    private static void publishArticle(String str, String url) {
        Document page = Jsoup.parse(str);
        Element siteNameEle = page.selectFirst(".site-name");
        Element imgcoverEle = page.selectFirst(".img-cover");
        Element aEle = page.selectFirst(".site-go-url a");
        Element descriptionEle = page.selectFirst(".site-body div p");
        Element bodyEle = page.selectFirst(".panel-body");
        Elements tagEles = page.select(".site-body a");

        String sitename = siteNameEle.text();
        String description = descriptionEle.text();
        String img = imgcoverEle.attr("data-src");
        if (img != null && img.startsWith("//")) {
            img = "https:" + img;
        }

        Elements elements = bodyEle.select("img");
        for (Element ele : elements) {
            String datasrc = ele.attr("data-src");
            if (datasrc.contains("www.moooyu.com")) {
                File file = new File("d:" + datasrc.substring(datasrc.indexOf("www.moooyu.com") + "www.moooyu.com".length()));
                file.getParentFile().mkdirs();
                try {
                    HttpClientUtil.download(datasrc, file.getPath());
                    datasrc = datasrc.replace("http:", "https:").replace("www.moooyu.com", "image.youhang.site");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            ele.removeAttr("data-src");
            ele.attr("src", datasrc);
        }
        String contentStr = bodyEle.html().replace("https://www.naviai.cn", "");
        List<String> tags = new ArrayList<>();
        for (Element tagEle : tagEles) {
            if (tagEle.attr("href") != null && tagEle.attr("href").startsWith("https://www.naviai.cn/favorites/")) {
                tags.add(tagEle.text().trim());
            }
        }
        String tag = tags.stream().collect(Collectors.joining("、"));

        HttpClientUtil.COOKIE = "wordpress_86a9106ae65537651a8e456835b316ab=admin%7C1703171958%7CTEPExLTIHmzBpB6fGWanqv1oYGTTdutOeKuwiqxX1Ej%7C9e8d90c1246014d55eed8c367c089f45a2fa
01c16424b8bf543ac01b831bcd8d; night=0; SESSION=N2Q2MWJkMDEtZDhkOC00ZTE3LTk5NWYtYjA3NmQxNzRkNzQ0; wordpress_test_cookie=WP+Cookie+check; wp-settings-1=advImgDetails%3Dhide%26editor
%3Dtinymce; wp-settings-time-1=1701954496; wp_lang=zh_CN; wordpress_logged_in_86a9106ae65537651a8e456835b316ab=admin%7C1703171958%7CTEPExLTIHmzBpB6fGWanqv1oYGTTdutOeKuwiqxX1Ej%7C
61c4a5cf4bc19edcee7243fe096b5a3802be5e5440f267a4342cb08ffcb6a04c";
        Map param = new HashMap();
        String content = HttpClientUtil.get("http://localhost/wp-admin/post-new.php?post_type=sites", null);
        Document document = Jsoup.parse(content);
        Element form = document.selectFirst("#post");
        Elements nameEles = form.select("[name]");
        for (Element nameEle : nameEles) {
            param.put(nameEle.attr("name"), nameEle.attr("value"));
        }
        param.put("post_title", sitename);
        param.put("content", contentStr);
        param.put("tax_input[sitetag]", tag);
        param.put("sites_post_meta[_sites_sescribe]", description);
        param.put("sites_post_meta[_thumbnail]", img);
        param.put("sites_post_meta[_sites_link]", url);
        param.put("visibility", "public");
        param.put("sites_post_meta[_sites_type]", "sites");
        param.put("tax_input[favorites][]", "3505");


        String resStr = HttpClientUtil.doPostForm("http://localhost/wp-admin/post.php", param);
        System.out.println(resStr);
    }

    
}

HttpClientUtil.COOKIE 浏览器登录成功后提取并程序设置cookie

通过 String content = HttpClientUtil.get("http://localhost/wp-admin/post-new.php?post_type=sites", null);得到文章提交表单

通过HttpClientUtil.doPostForm("http://localhost/wp-admin/post.php", param);提交文章表单,上面的参数获取与设置都在上面了

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值