为了实现同步腾讯云博客的阅读量,我就写了这篇文章。
备注:你需要会在服务器搞定时任务。知道cUrl是什么。
免责声明:本博客演示的地址,会随时停止,不得作为你实际生产抓包使用!因恶意爬虫导致的一切责任均有你自己承担!
腾讯云博客原始请求抓包cURL
具体怎么抓包,我就不描述了:找到网页,然后刷新浏览器,看那个接口返回你要的数据了。
我抓包信息如下:
curl 'https://cloud.tencent.com/developer/api/user/rank' \
-H 'accept: */*' \
-H 'accept-language: zh-CN,zh;q=0.9' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'cookie: qcommunity_identify_id=7RwtnPExliPEzqE07eBS7; qcloud_uid=M-BKt29NsS6v; language=zh; qcmainCSRFToken=qpCIFG2Zi2VD; qcloud_visitId=c880bf4e8f77ccbd868c5e1f69f30f35; qcloud_outsite_refer=https://www.zanglikun.com; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221935729864181b-0135ba781948b1-26011851-1638720-19357298642d4c%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkzNTcyOTg2NDE4MWItMDEzNWJhNzgxOTQ4YjEtMjYwMTE4NTEtMTYzODcyMC0xOTM1NzI5ODY0MmQ0YyJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%2C%22%24device_id%22%3A%221935729864181b-0135ba781948b1-26011851-1638720-19357298642d4c%22%7D; intl=; _ga=GA1.2.1963929058.1732334160; _gat=1; qcstats_seo_keywords=%E5%93%81%E7%89%8C%E8%AF%8D-%E5%93%81%E7%89%8C%E8%AF%8D-%E8%85%BE%E8%AE%AF%E4%BA%91; _gcl_au=1.1.760994683.1732334160; qcloud_from=qcloud.outside.seo-1732334160061; trafficParams=***%24%3Btimestamp%3D1732334267072%3Bfrom_type%3Dserver%3Btrack%3D225baed5-1c2b-4203-978a-94c1f7b31b5a%3B%24***' \
-H 'origin: https://cloud.tencent.com' \
-H 'pragma: no-cache' \
-H 'priority: u=1, i' \
-H 'referer: https://cloud.tencent.com/developer/user/6862050' \
-H 'sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
-H 'sec-fetch-dest: empty' \
-H 'sec-fetch-mode: cors' \
-H 'sec-fetch-site: same-origin' \
-H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \
--data-raw '{"uid":6862050,"year":2024}'
本地调试可行,响应结果是:
NGINX配置 防止前端浏览器跨域
因为前端浏览器发起的请求会有浏览器的同源策略检测,实际发送的请求域名是腾讯云的,我们是本地的url。两者不相同,就会有跨域报错。所以我采用本地Nginx作为解决办法。
location /txy/rank {
proxy_pass https://cloud.tencent.com/developer/api/user/rank;
# 添加 CORS 头
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization';
# 处理 OPTIONS 请求
if ($request_method = 'OPTIONS') {
return 204;
}
}
初次尝试HTML代码
下文是GPT帮我写的!因为上文有了NGINX来解决跨域问题。所以我们HTML的域名就是我们Nginx的地址。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch User Article Read Number 初次尝试</title>
</head>
<body>
<h1>User Article Read Number 初次尝试</h1>
<div id="articleReadNum">Loading...</div>
<script>
// Function to fetch data from the API
async function fetchUserData() {
try {
const response = await fetch('https://www.你的域名.com/txy/rank', {
method: 'POST',
headers: {
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json'
},
body: JSON.stringify({
uid: 6862050,
year: 2024
})
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
// Assuming the structure of your response
const articleReadNum = data.user.articleReadNum;
// Display the articleReadNum
document.getElementById('articleReadNum').innerText = articleReadNum;
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
document.getElementById('articleReadNum').innerText = 'Error fetching data';
}
}
// Call the function to fetch data when the page loads
fetchUserData();
</script>
</body>
</html>
</title>
</head>
<body>
</body>
</html>
HTML对应的效果展示
User Article Read Number 初次尝试
Loading...