小红书网页爬取id和标题

xiaohongshuBurst

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         小红书网页爬取id和标题
// @version      0.2
// @namespace    http://tampermonkey.net/
// @description  xiaohongshuBurst
// @author       OrlosZiming
// @match        http*://www.xiaohongshu.com/explore*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @require      http://code.jquery.com/jquery-3.x-git.min.js
// @license MIT
// ==/UserScript==
(function() {
    'use strict';
    window.onload = function() {
        // 设置通用IP地址前缀
        var articleUrl = "https://www.xiaohongshu.com";

        // 获取作者名及跳转连接
        var authorNameHref = articleUrl + $("a.name:first").attr("href");
        var authorNameText = $("a.name:first").text();

        var intervalId;

        var articleData = {}; // 存储多个标题和链接的对象
        // 自动下拉页面
        var scrollHeight = document.body.scrollHeight;
        var currentScroll = 0;
        var scrollStep = 1000; // 每次滚动的步长
        var scrollInterval = 1000; // 每次滚动的时间间隔

        function autoScroll() {
            console.log("down!"+currentScroll)
                window.scrollTo(0, currentScroll);
                currentScroll += scrollStep;
        }

        // 开始自动下拉

        function getArticleInfo() {
            // 获取笔记时间
            var articleDate = $("div.date").text();
            articleDate = articleDate.replace(/-/g,'/');

            // 遍历每个文章元素
            // 遍历每个包含指定 href 的链接元素
            $('a[href*="/explore/"]').each(function(index, element) {
                // 获取链接元素
                var linkElement = $(element);

                // 获取链接
                var articleLink = linkElement.attr("href");
                console.log("Link:", articleLink);

                // 提取 articleId
                var articleId = articleLink.replace('/explore/', '').split('?')[0];
                console.log("Article ID:", articleId);

                // 获取对应的标题元素
                var titleElement = linkElement.closest('div').find('.title');

                // 获取标题文本
                var articleTitle = titleElement.text();
                console.log("Title:", articleTitle);

                // 输出链接和标题
                console.log("Link:", articleLink, "Title:", articleTitle);

                // 将当前标题和链接的键值对添加到新窗口的内容中
                $(myWindow.document.body).append('<div>' + articleId + ': ' + articleTitle + '</div>');
            });

        }

        // 开始定时获取笔记信息
        intervalId = setInterval(getArticleInfo, 200);

        // 遍历配图
        $("div.note-scroller").before("<div id='img-location'></div>");
        $(".swiper-slide.zoom-in:not(.swiper-slide-duplicate)").each(function(i, item) {
            var style = $(item).attr('style');
            var url1 = style.replace('background-image: url("', '');
            var url = url1.replace('");', '');
            $("div#img-location").append("<a href='"+url+"'><image class='img' style='width: 100px;' src='"+url+"'></a>");
        });

        // 获取视频
        var videoSrc = $(".browser-player").attr("src");

        // 获取当前窗口的url
        let websiteNow = location.href;
        var loc = websiteNow.indexOf("?");
        if (loc != -1) {
            websiteNow = websiteNow.substr(0, loc);
        }

        // 打开新窗口
        var myWindow = window.open("", "MsgWindow");

        // 将作者名、文章时间、配图或视频、文章标题、内容写入新窗口
        $(myWindow.document.body).append(websiteNow);
        $(myWindow.document.body).append(
            '<div><a href="'+authorNameHref+'">'+authorNameText+'</a></div>' +
            '<div>'+articleDate+'</div>'
        );
        if ($(".browser-player").length > 0) {
            $(myWindow.document.body).append(
                '<div><a href="'+videoSrc+'">'+videoSrc+'</a></div>'
            );
        } else {
            $(myWindow.document.body).append(img);
        }
    };
})();