在编译命令中添加 -lwininet 参数 #include <windows.h> #include <wininet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <conio.h> #include <time.h> #pragma comment(lib, "wininet.lib") #if defined(__MINGW32__) || defined(__MINGW64__) #ifndef INTERNET_FLAG_IGNORE_CERT_ERRORS #define INTERNET_FLAG_IGNORE_CERT_ERRORS 0x00000080 #endif #define safe_strcpy(dst, dst_size, src) do{strncpy(dst,src,dst_size-1);dst[dst_size-1]='\0';}while(0) #else #define safe_strcpy(dst, dst_size, src) strcpy_s(dst,dst_size,src) #endif bool copy_to_clipboard(const char*); void random_delay(int, int); std::string utf8_to_console(const char*); std::string decode_unicode(const std::string&); char* download_url_with_session(HINTERNET, const char*, const char*, int*); std::string parse_chapter_json(const char*); std::string filter_novel_content(const char*); std::string replace_continuous_non_chinese_with_dot(const char* content); bool copy_to_clipboard(const char* text) { if (!text || !*text)return 0; if (!OpenClipboard(NULL))return 0; EmptyClipboard(); size_t len = strlen(text) + 1; HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len); if (!hMem) { CloseClipboard(); return 0; } char* pMem = (char*)GlobalLock(hMem); if (pMem) { strcpy(pMem, text); GlobalUnlock(hMem); } else { GlobalFree(hMem); CloseClipboard(); return 0; } SetClipboardData(CF_TEXT, hMem); CloseClipboard(); return 1; } void random_delay(int min_ms, int max_ms) { static bool f = 1; if (f) { srand((unsigned)time(NULL)^GetCurrentProcessId()); f = 0; } int d = min_ms + rand() % (max_ms - min_ms + 1); int j = rand() % 500; d += rand() % 2 ? j : -j; d = d < 1000 ? 1000 : d; Sleep(d); } std::string utf8_to_console(const char* s) { if (!s || !*s)return ""; int wl = MultiByteToWideChar(65001, 0, s, -1, NULL, 0); if (wl <= 0)return std::string(s); wchar_t* w = new wchar_t[wl]; MultiByteToWideChar(65001, 0, s, -1, w, wl); int gl = WideCharToMultiByte(936, 0, w, -1, NULL, 0, NULL, NULL); if (gl <= 0) { delete[]w; return std::string(s); } char* g = new char[gl]; WideCharToMultiByte(936, 0, w, -1, g, gl, NULL, NULL); std::string r(g); delete[]w; delete[]g; return r; } std::string decode_unicode(const std::string& s) { std::string r; size_t p = 0; while (p < s.size()) { if (s[p] == '\\' && s[p + 1] == 'u' && p + 5 < s.size()) { wchar_t c = (wchar_t)strtol(s.substr(p + 2, 4).c_str(), NULL, 16); char b[10] = {0}; WideCharToMultiByte(936, 0, &c, 1, b, 10, NULL, NULL); r += b; p += 6; } else { r += s[p++]; } } return r; } char* download_url_with_session(HINTERNET h, const char* u, const char* ref, int* l) { *l = 0; if (!h || !u || !*u)return NULL; DWORD t = 20000; InternetSetOptionA(h, INTERNET_OPTION_CONNECT_TIMEOUT, &t, sizeof(t)); InternetSetOptionA(h, INTERNET_OPTION_RECEIVE_TIMEOUT, &t, sizeof(t)); InternetSetOptionA(h, INTERNET_OPTION_SEND_TIMEOUT, &t, sizeof(t)); std::string hd = "Accept: application/json,*/*\r\nAccept-Language: zh-CN,zh;q=0.9\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nCookie: Hm_lvt_123=1710000000\r\nOrigin: https://m.bqg930.cc\r\nSec-Ch-Ua: \"Not_A Brand\";v=\"8\"\r\nSec-Ch-Ua-Mobile: ?0\r\nSec-Ch-Ua-Platform: \"Windows\"\r\nSec-Fetch-Dest: empty\r\nSec-Fetch-Mode: cors\r\nSec-Fetch-Site: same-origin\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0 Safari/537.36\r\n"; if (ref && *ref) { hd += "Referer: "; hd += ref; hd += "\r\n"; } DWORD f = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_ERRORS; HINTERNET hu = InternetOpenUrlA(h, u, hd.c_str(), -1, f, 0); if (!hu) { printf("URL\u5931\u8d25:%s \u9519\u8bef:%d\n", u, GetLastError()); return NULL; } char* b = (char*)malloc(4 * 1024 * 1024); if (!b) { InternetCloseHandle(hu); return NULL; } DWORD r = 0; int n = 0; while (InternetReadFile(hu, b + n, 8192, &r) && r) { n += r; if (n >= 4 * 1024 * 1024 - 8192)break; } b[n] = 0; InternetCloseHandle(hu); *l = n; random_delay(1500, 3000); return b; } std::string parse_chapter_json(const char* j) { if (!j || !*j)return "\u7a7a"; std::string s = j; size_t st = s.find("\"txt\":\""); if (st == std::string::npos) { st = s.find("\"content\":\""); if (st == std::string::npos)return "\u65e0\u6b63\u6587"; st += 10; } else st += 6; size_t ed = st; int e = 0; bool ok = 0; while (ed < s.size() && ed < st + 200000) { if (s[ed] == '\\') { e++; ed++; continue; } if (s[ed] == '"' && e % 2 == 0) { ok = 1; break; } e = 0; ed++; } if (!ok || ed <= st) { size_t a = s.find("\",", st), b = s.find("}", st); ed = a != std::string::npos ? a : b; if (ed == std::string::npos || ed <= st)ed = st + 10000 < s.size() ? st + 10000 : s.size() - 1; } std::string c = s.substr(st, ed - st); c = utf8_to_console(c.c_str()); c = decode_unicode(c); size_t p = 0; while ((p = c.find(" ", p)) != std::string::npos) { c.replace(p, 6, " "); p++; } p = 0; while ((p = c.find("<", p)) != std::string::npos) { c.replace(p, 4, "<"); p++; } p = 0; while ((p = c.find(">", p)) != std::string::npos) { c.replace(p, 4, ">"); p++; } p = 0; while ((p = c.find("\\n", p)) != std::string::npos) { c.replace(p, 2, "\n"); p++; } p = 0; while ((p = c.find("\\r", p)) != std::string::npos) { c.replace(p, 2, "\r"); p++; } p = 0; while ((p = c.find("\\t", p)) != std::string::npos) { c.replace(p, 2, " "); p++; } p = 0; while ((p = c.find("\\\\", p)) != std::string::npos) { c.replace(p, 2, "\\"); p++; } return c.empty() ? "\u65e0\u6b63\u6587" : c; } std::string filter_novel_content(const char* r) { if (!r || !*r)return "\u65e0\u5185\u5bb9"; std::string s = r, f; size_t p = 0; while (p < s.size()) { size_t e = s.find('\n', p); if (e == std::string::npos)e = s.size(); std::string l = s.substr(p, e - p); p = e + 1; size_t t = l.find('<'); while (t != std::string::npos) { size_t te = l.find('>', t); if (te == std::string::npos)break; l.erase(t, te - t + 1); t = l.find('<'); } size_t fi = l.find_first_not_of(" \t\r\n"), la = l.find_last_not_of(" \t\r\n"); if (fi == std::string::npos || la == std::string::npos)continue; l = l.substr(fi, la - fi + 1); if (l.find("http") != std::string::npos || l.find("\u6536\u85cf") != std::string::npos || l.find("\u7b14\u8da3\u9601") != std::string::npos || l.find("time") != std::string::npos || l.find("md5") != std::string::npos || (l == "}"))continue; if (l.size() > 2)f += l + "\n\n"; } return f.empty() ? "\u65e0\u5185\u5bb9" : f; } // 核心功能:连续非中文字符转句号 + 空格/换行不变 + 保留中文+标点+引号 std::string replace_continuous_non_chinese_with_dot(const char* content) { if (!content || !*content) return "\u65e0\u5185\u5bb9"; // 转换为宽字符(适配GBK/936编码) int wlen = MultiByteToWideChar(936, 0, content, -1, NULL, 0); if (wlen <= 0) return std::string(content); wchar_t* wstr = new wchar_t[wlen]; MultiByteToWideChar(936, 0, content, -1, wstr, wlen); std::wstring result_wstr; bool in_non_chinese = false; // 标记是否处于连续非中文字符段 // 定义中文符号的Unicode编码值 const wchar_t LEFT_CORNER_BRACKET = 0x300C; // 「 const wchar_t RIGHT_CORNER_BRACKET = 0x300D; // 」 const wchar_t LEFT_DOUBLE_QUOTE = 0x201C; // “ const wchar_t RIGHT_DOUBLE_QUOTE = 0x201D; // ” const wchar_t LEFT_SINGLE_QUOTE = 0x2018; // ‘ const wchar_t RIGHT_SINGLE_QUOTE = 0x2019; // ’ const wchar_t CHINESE_DOT = 0x3002; // 。 for (int i = 0; wstr[i] != L'\0'; i++) { wchar_t c = wstr[i]; if (c == L' ' || c == L'\n' || c == L'\r' || c == L'\t') { result_wstr += c; in_non_chinese = false; continue; } bool is_keep = false; // 1. 中文汉字 if (c >= 0x4E00 && c <= 0x9FFF) is_keep = true; // 2. 中文标点 else if ((c >= 0x3000 && c <= 0x303F) || (c >= 0xFF00 && c <= 0xFFEF)) is_keep = true; // 3. 引号(中英文) else if (c == L'\"' || c == L'\'' || c == LEFT_CORNER_BRACKET || c == RIGHT_CORNER_BRACKET || c == LEFT_DOUBLE_QUOTE || c == RIGHT_DOUBLE_QUOTE || c == LEFT_SINGLE_QUOTE || c == RIGHT_SINGLE_QUOTE) is_keep = true; if (is_keep) { // 遇到保留字符:结束非中文段,加句号(如果有) if (in_non_chinese) { result_wstr += CHINESE_DOT; in_non_chinese = false; } result_wstr += c; } else { // 非保留字符:标记为连续非中文段 in_non_chinese = true; } } // 处理末尾的连续非中文字符段 if (in_non_chinese) { result_wstr += CHINESE_DOT; } // 转换回多字节字符串 int mlen = WideCharToMultiByte(936, 0, result_wstr.c_str(), -1, NULL, 0, NULL, NULL); char* mstr = new char[mlen]; WideCharToMultiByte(936, 0, result_wstr.c_str(), -1, mstr, mlen, NULL, NULL); std::string result = mstr; delete[] wstr; delete[] mstr; return result; } int main() { SetConsoleCP(936); SetConsoleOutputCP(936); const char* bid = "40196"; int cid; char u1[256], u2[256]; HINTERNET h = InternetOpenA("Mozilla/5.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if (!h) { printf("\u7f51\u7edc\u5931\u8d25:%d\n", GetLastError()); system("pause>nul"); return -1; } while (1) { scanf("%d", &cid); if (cid <= 0)break; char n[32] = {0}; sprintf(n, "%d", cid); copy_to_clipboard(n); sprintf(u1, "https://m.bqg930.cc/book/%s/%d.html", bid, cid); sprintf(u2, "https://m.bqg930.cc/api/chapter?id=%s&chapterid=%d", bid, cid); int l = 0; char* d = download_url_with_session(h, u2, u1, &l); if (!d || l < 100) { printf("\u8bf7\u6c42\u5931\u8d25\n"); free(d); continue; } std::string t = parse_chapter_json(d); t = filter_novel_content(t.c_str()); // 调用核心替换函数 // t = replace_continuous_non_chinese_with_dot(t.c_str()); system("cls"); printf("\n===== \u7ae0\u8282%d =====\n\n%s\n", cid, t.c_str()); free(d); printf("-------------------------------------\n"); } InternetCloseHandle(h); printf("\u9000\u51fa\n"); system("pause>nul"); return 0; }
Note.ms
/jojo2011code