今天上课,写报告,准备打算复习,然后完成跨域的请求问题
 function fetchNews(url) {
            console.log("开始了开始了奥里给");
            const proxyUrl = 'https://api.allorigins.win/get?url=';
            // const url = 'https://www.eco.gov.cn/subclass/ongoing_learning.html';
            fetch(proxyUrl + encodeURIComponent(url))
                .then(response => response.json())
                .then(data => {
                    console.log("第二个");
                    console.log(proxyUrl + encodeURIComponent(url));
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(data.contents, 'text/html');
                    const newsContainer = document.getElementById('news-container');
                    newsContainer.innerHTML = ''; // 清空之前的内容

                    doc.querySelectorAll('.m-list li .info').forEach(item => {
                        const titleTag = item.querySelector('h3.title a');
                        const descTag = item.querySelector('p.desc a');

                        if (titleTag && descTag) {
                            const title = titleTag.textContent.trim();
                            let link = titleTag.href;
                            const description = descTag.textContent.trim();
                            link = link.replace('http://101.42.22.40:3000/', 'https://www.eco.gov.cn');
                            console.log(link);
                            console.log(title);
                            const newsItem = `
                        <div class="boxall">
                            <div class="alltitle"><i class="fas fa-fire icon"></i><a href="${link}" target="_blank" style="font-size: 30px; font-family: 楷体, sans-serif;color:rgb(29, 124, 193)">${title}</a></div>
                            <div class="allnav2">
                                <p style="text-align: right; color: orange; font-size: smaller;">--生态中国网</p>
                            </div>
                         </div>
                    `;
                            console.log(newsItem);
                            newsContainer.insertAdjacentHTML('beforeend', newsItem);
                        }
                    });
                    console.log("总的");
                    console.log("News container:", newsContainer);
                })
                .catch(error => console.error('Error fetching news:', error));
        }

        document.addEventListener('DOMContentLoaded', function () {
            const urls = [
                'https://www.eco.gov.cn/subclass/ongoing_learning.html',
                'https://www.eco.gov.cn/subclass/ongoing_learning/1.html',
                'https://www.eco.gov.cn/subclass/ongoing_learning/2.html'
            ];
            urls.forEach(url => fetchNews(url)); // 遍历每个URL并调用fetchNews函数
            // fetchNews(); // 初次加载时获取数据
            // setInterval(fetchNews, 1800000); // 每30分钟获取一次数据
        });

 

posted on 2024-06-03 23:32  许七安gyg  阅读(3)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });