哈哈,被骗进来了吧,老铁,这个玩意儿纯属脱裤子放屁,多此一举,浏览器就能打开的事情,没必要再拿文章地址再去解锁看内容,闲来没事研究学习下解析html内容,有兴趣的研究学习下
项目会涉及到以下几个核心部分:
- 文章抓取:通过 HTTP 请求抓取 优快云 文章页面。
- 会员身份模拟:通过存储 优快云 会员账号的 Cookie 或 Token 来访问会员专属文章。
- 解锁和下载功能:用户输入文章链接后,判断是否为会员文章,如果是会员文章,则可以解锁并下载。
目录
2.1 新建 ASP.NET Core Web API 项目
实现步骤
1. 项目结构
- ASP.NET Core 后端:用于处理 HTTP 请求,抓取和解锁 优快云 文章。
- 前端页面:一个简单的vue页面,允许用户输入文章 URL 并提供解锁和下载功能。
2. 后端代码(ASP.NET Core)
2.1 新建 ASP.NET Core Web API 项目
首先,你需要创建一个 ASP.NET Core Web API 项目:
dotnet new webapi -n CsdnUnlocker
cd CsdnUnlocker
2.2 安装所需的 NuGet 包
你需要 HtmlAgilityPack 来处理 HTML 页面抓取:
dotnet add package HtmlAgilityPack --version 1.11.37
2.3 创建 CsdnService 服务类
在 Services
文件夹中创建 CsdnService.cs
,该类负责处理与 优快云 的交互。
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace CsdnUnlocker.Services
{
public class CsdnService
{
private static readonly HttpClient _httpClient = new HttpClient();
// 你可以通过Cookie模拟登录,获取会员内容
private readonly string CsdnMemberCookie = "YOUR_优快云_MEMBER_COOKIE_HERE";
public async Task<bool> IsMemberArticleAsync(string articleUrl)
{
var request = new HttpRequestMessage(HttpMethod.Get, articleUrl);
request.Headers.Add("Cookie", CsdnMemberCookie);
var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
return false;
}
var content = await response.Content.ReadAsStringAsync();
// 判断页面是否包含“会员专享”等字样
return content.Contains("会员专享");
}
public async Task<string> UnlockArticleAsync(string articleUrl)
{
var request = new HttpRequestMessage(HttpMethod.Get, articleUrl);
request.Headers.Add("Cookie", CsdnMemberCookie);
var response = await _httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
return null;
}
var content = await response.Content.ReadAsStringAsync();
var document = new HtmlDocument();
document.LoadHtml(content);
// 提取完整文章内容
var articleNode = document.DocumentNode.SelectSingleNode("//div[@class='article-content']");
if (articleNode != null)
{
return articleNode.InnerHtml;
}
return null;
}
}
}
2.4 创建 API 控制器
在 Controllers
文件夹中创建 CsdnController.cs
,提供两个 API:
- 检查文章是否为会员文章。
- 解锁会员文章。
using HtmlAgilityPack; namespace CsdnUnlocker { public class CsdnService { private static readonly HttpClient _httpClient = new HttpClient(); // 你可以通过Cookie模拟登录,获取会员内容 private readonly string CsdnMemberCookie = "802e77502c9c4e038bc2c32353be5ad1"; public async Task<bool> IsMemberArticleAsync(string articleUrl) { var request = new HttpRequestMessage(HttpMethod.Get, articleUrl); request.Headers.Add("Cookie", CsdnMemberCookie); var response = await _httpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { return false; } var content = await response.Content.ReadAsStringAsync(); // 判断页面是否包含“会员专享”等字样 return content.Contains("会员专享"); } public async Task<string> UnlockArticleAsync(string articleUrl) { var request = new HttpRequestMessage(HttpMethod.Get, articleUrl); request.Headers.Add("Cookie", CsdnMemberCookie); var response = await _httpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { return null; } var content = await response.Content.ReadAsStringAsync(); var document = new HtmlDocument(); document.LoadHtml(content); // 提取完整文章内容 var articleNode = document.DocumentNode.SelectSingleNode("//div[@class='article-content']"); if (articleNode != null) { return articleNode.InnerHtml; } return null; } } }
2.5 配置服务和路由
在
Startup.cs
或Program.cs
中,添加对CsdnService
的依赖注入,并确保启用了静态文件中间件,以便用户可以下载文章。using CsdnUnlocker; using Microsoft.Extensions.PlatformAbstractions; using Microsoft.OpenApi.Models; internal class Program { private static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddScoped<CsdnService>(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); var basePath = PlatformServices.Default.Application.ApplicationBasePath; var xmlPath = Path.Combine(basePath, "CsdnUnlocker.xml"); c.IncludeXmlComments(xmlPath); }); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1")); } app.UseHttpsRedirection(); app.UseAuthorization(); app.UseStaticFiles(); app.UseRouting(); app.MapControllers(); app.UseCors(builder => { builder.WithOrigins("http://localhost:8080").AllowAnyMethod().AllowAnyHeader(); }); app.Run(); } }
3. 前端部分
<template> <div> <el-input v-model="csdnUrl" placeholder="请输入优快云文章URL"></el-input> <el-button type="primary" @click="checkArticle">检查文章</el-button> <div v-if="unlockable"> <el-alert title="此文章为会员文章,可以解锁" type="info"></el-alert> <el-button type="success" @click="unlockArticle">解锁文章</el-button> </div> <div v-if="downloadUrl"> <el-button type="primary" :href="downloadUrl" download>下载解锁后的文章</el-button> </div> </div> </template> <script> import axios from 'axios'; export default { name: 'App', data() { return { csdnUrl: '', unlockable: false, downloadUrl: '' }; }, methods: { checkArticle() { axios.post('http://localhost:7294/api/Csdn/check', { url: this.csdnUrl }, { headers: { 'Content-Type': 'application/json' // 设置请求头为 JSON } }).then(response => { if (response.data.unlockable) { this.unlockable = true; } else { this.$message.error('该文章无需解锁或无权限解锁'); } }); }, unlockArticle() { axios.post('http://localhost:7294/api/Csdn/unlock', { url: this.csdnUrl }).then(response => { this.downloadUrl = response.data.downloadUrl; this.$message.success('解锁成功,点击下载'); }); } } } </script>
4. 部署与运行
1.运行项目:在项目根目录下运行以下命令,启动 ASP.NET Core 应用。
dotnet run
2.访问前端页面:打开浏览器,访问 https://localhost:5001
,输入 优快云 文章链接进行测试。
5. 注意事项
- 优快云 会员凭证:需要确保你的 优快云 会员账号的 Cookie 是最新且有效的,过期后需要更新。
- 合法性与合规性:请遵守 优快云 平台的服务条款及相关法律,确保不会滥用该功能,造成不必要的法律风险。