我正在尝试通过私有api从YouTube下载评论,但由于某些原因,对api的请求无法正常使用。它应该返回JSON,其中包含有关视频下评论的信息,但对我来说它什么也不返回。从字面上看没什么。在具有相同参数和标题的邮递员中,所有功能都可以完美运行。我认为我没有正确使用curl,但我不知道我在哪里错。
我的下载代码:
QMap<QString, QString> commentsChunk = {
{"Accept", "*/*"},
{"Host", "www.youtube.com"},
{"Content-Type", "application/x-www-form-urlencoded"},
{"Origin", "https://www.youtube.com"},
{"DNT", "1"},
{"Pragma", "no-cache"},
{"Cache-control", "no-cache"},
{"TE", "Trailers"},
};
QString dir = KTools::Options::configPath + "/tmp";
cc->setErrFile(dir, "errfile.txt");
cc->currHeaderMode = KTools::Enums::Curl::HeaderMode::None;
cc->currCookieMode = KTools::Enums::Curl::CookieMode::GetAllTimes;
cc->cookieFileName = "cookieYoutube.txt";
cc->cookiePath = KTools::Options::configPath;
cc->setOptions();
QByteArray data = cc->request(params.value("url").toString() + "/videos");
KTools::HtmlAst::Object htmlObj = KTools::HtmlAst::Object();
htmlObj.makeAst(data);
QString xsrfToken = QUrl::toPercentEncoding(htmlObj.arrsAndObjs.objects[0].value("XSRF_TOKEN").toString());;
commentsChunk["X-YouTube-Client-Version"] = htmlObj.arrsAndObjs.objects[0].value("INNERTUBE_CONTEXT_CLIENT_VERSION").toString();
commentsChunk["X-YouTube-Client-Name"] = htmlObj.arrsAndObjs.objects[0].value("INNERTUBE_CONTEXT_CLIENT_NAME").toVariant().toString();
QJsonArray videosInfo = htmlObj.arrsAndObjs.objects[3].value("contents").toObject().value("twoColumnBrowseResultsRenderer").toObject().value("tabs").toArray()[1].toObject().value("tabRenderer").toObject().value("content").toObject().value("sectionListRenderer").toObject().value("contents").toArray()[0].toObject().value("itemSectionRenderer").toObject().value("contents").toArray()[0].toObject().value("gridRenderer").toObject().value("items").toArray(); // line 45 "items":[...]
QVector<QString> videosId;
for (int i = 0; i < videosInfo.size(); i++)
{
videosId.append(videosInfo[i].toObject().value("gridVideoRenderer").toObject().value("videoId").toString());
}
data = cc->request("https://www.youtube.com/watch?v=" + videosId[1]);
htmlObj = KTools::HtmlAst::Object();
htmlObj.makeAst(data);
QJsonArray continuations = htmlObj.arrsAndObjs.objects[5].value("contents").toObject().value("twoColumnWatchNextResults").toObject().value("secondaryResults").toObject().value("secondaryResults").toObject().value("continuations").toArray();
QVector<QJsonObject> commentsJsons;
cc->restartSession();
cc->setRequestType(KTools::Enums::Curl::RequestType::Post);
cc->currHeaderMode = KTools::Enums::Curl::HeaderMode::Custom;
QVector<QVector<QVector<QString>>> regexResult;
KTools::ExForString::executeRegex(data, {{"([^\"]+___________[^\"]+)\",\"[^\"]+\":\"([^\"]+)"}}, regexResult);
cc->setHeader(commentsChunk);
cc->setOptions();
QString tmp = QUrl::toPercentEncoding(regexResult[0][0][1]);
QString tmp2 = QUrl::toPercentEncoding(regexResult[0][0][2]);
cc->currPostParam = "session_token=" + xsrfToken;
data = cc->request("https://www.youtube.com/comment_service_ajax?action_get_comments=1&pbj=1&ctoken=" + tmp + "&continuation=" + tmp + "&itct=" + tmp2);
KTools::File::writeFile((tmp + "\n" + tmp2 + "\n" + cc->currPostParam).toUtf8(), dir, "requestParams.txt");
cc->unsetErrFile();
QString nope;
我的卷曲包装代码:
头文件:https://pastebin.com/beg1MYgG
实施文件:https://pastebin.com/Dv4i823P
卷曲记录:https://pastebin.com/xXnsQEqY
更新:感谢评论中的建议,我找到了我的问题。由于某种原因,函数std :: string :: c_str()不能正常工作,换句话说,给出了随机字符。我该如何解决?
答案 0 :(得分:0)
最后只是UB。
width
卷曲存储不是字符串,而是对字符串的引用。但是返回的临时字符串bu c_str()在if语句的末尾销毁。