当我爬到我的大学网站的一部分时,出现了错误。
Failed to load resource: net::ERR_CONNECTION_REFUSED (localhost:3000/:1)
createError.js:16 Uncaught (in promise) Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:99)
我已经尝试过其他事情,但我不确定为什么它会显示“加载...”并且在控制台中出现错误。
该站点正在运行,但爬网无法正常运行并显示“加载...”
这是kangnam.js:
const axios = require('axios');
const cheerio = require('cheerio');
const log = console.log;
const express = require("express");
const cors = require("cors");
const PORT = 4000;
const app = express();
app.use(cors());
const getHTML = async () => {
try {
return await axios.get('https://web.kangnam.ac.kr', {
headers: {
Accept: 'text/html'
}
});
} catch (error) {
console.log(error);
}
};
app.get("/", (req, res) => {
getHTML()
.then(html => {
const $ = cheerio.load(html.data);
const $allNotices = $("ul.tab_listl div.list_txt");
let resultArr = [];
$allNotices.each(function(idx, element) {
let itemObj = {
title : $(this).children('a').attr('title'),
url : $(this).children('a').attr('href'),
};
resultArr.push(itemObj);
});
resultArr.forEach((element) => {
console.log(`현재 ${element._title}의 현황 : ${element._url}`);
});
return resultArr;
// const data = ulList.filter(n => n.title);
// return data;
}). then((data) => res.send(data));
});
app.listen(PORT, () =>
console.log(`Example app listening at http://localhost:${PORT}`)
);
这是NoticesList.js:
import React, {useEffect, useState} from 'react';
import styled from 'styled-components';
import NoticesItem from './NoticesItem';
import axios from "axios";
const NoticesListBlock = styled.div`
box-sizing: border-box;
padding-bottom: 3rem;
width: 768px;
margin: 0 auto;
margin-top: 2rem;
`;
const sampleArticle = {
title: 'title',
url: 'https://google.com',
};
const NoticesList = () => {
const [data, setData] = useState(null);
useEffect(() => {
const getData = async() => {
const datas = await axios.get("http://localhost:3000/");
setData(datas.data);
};
getData();
}, []);
useEffect(() => {
console.log(data);
}, [data]);
if(data === null){
return <div>Load....</div>;
}else{
console.log(data);
return (
<div>
{data.map((ele) => (
<>
<div>
{ele.title};
</div>
<br/>
</>
))}
</div>
);
};
};
export default NoticesList;
这是NoticesItem.js:
import React from 'react';
import styled from 'styled-components';
const NoticesItemBlock = styled.div`
display: flex;
.contents {
h6 {
margin: 15px;
a {
color: black;
}
}
}
& + & {
margin-top: 1.5rem;
}
`;
const NoticesItem = ({ article }) => {
const { title, url } = article;
return (
<NoticesItemBlock>
<div className="contents">
<h6>
<a href={url} target="_blank" rel="noopener noreferrer">
{title}
</a>
</h6>
</div>
</NoticesItemBlock>
);
};
export default NoticesItem;