如何将 IHTMLDocument2 ->get_body ->get_innerHTML 转换为小写字符串?
我正在尝试从 c++ 上的网页主体获取innerHTML,到目前为止我已经做到了:
// I get "Document" from a parameter when calling this code
BSTR bstrContent = NULL;
IHTMLElement *p = 0;
Document->get_body( &p );
if( p )
{
p->get_innerHTML( &bstrContent );
p->Release();
}
现在我需要将 bstrContent 转换为小写 std::string 或 LPSTR,我已经尝试过这个:
LPSTR pagecontent = NULL;
int responseLength = (int)wcslen(bstrContent);
pagecontent = new CHAR[ responseLength + 1 ];
wcstombs( pagecontent, bstrContent, responseLength);
但是“pagecontent”并不总是包含完整的innerHTML,只有第一个块。我即使它有效,我也不知道如何轻松地使其全部小写,使用 std::string 我会使用“transform”+“tolower”来做到这一点。
那么,如何将 bstrContent 转换为 std::string ?
I am trying to get the innerHTML from a webpage body on c++, I have this so far:
// I get "Document" from a parameter when calling this code
BSTR bstrContent = NULL;
IHTMLElement *p = 0;
Document->get_body( &p );
if( p )
{
p->get_innerHTML( &bstrContent );
p->Release();
}
Now I need to turn bstrContent into a lowercase std::string or LPSTR, I've tried this:
LPSTR pagecontent = NULL;
int responseLength = (int)wcslen(bstrContent);
pagecontent = new CHAR[ responseLength + 1 ];
wcstombs( pagecontent, bstrContent, responseLength);
But "pagecontent" does not always contain the full innerHTML, only a first chunk. I even if it worked, I don't know how to easily make it all lowercase, with a std::string I'd use "transform"+"tolower" to do it.
So, how can I turn bstrContent into a std::string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否完全理解你的问题。我不知道为什么 get_innerHTML 会给你一个不完整的主体,但你可以将 BSTR 转换为 std::string (假设你不需要支持 unicode,在这种情况下你应该使用 std ::wstring 无论如何)使用以下页面上找到的函数:
http://www .codeguru.com/forum/showthread.php?t=275978
如果您使用 ATL,还有 CA2W 转换实用程序,但我链接到您的函数更好,因为它至少支持 UTF8,如果相关的。
希望有帮助,
I'm not sure I fully understand your question. I don't know of any reason why get_innerHTML would give you an incomplete body, but you can convert a BSTR to a std::string (assuming you don't need to support unicode, in which case you should have been using a std::wstring anyway) using a function found on the following page:
http://www.codeguru.com/forum/showthread.php?t=275978
If you're using ATL there is also the CA2W conversion utility, but the function I linked you to is better since it'll at least support UTF8 if relevant.
Hope that helps,
如果您也有起始指针和结束指针,则 std::transform 工作正常。它适用于任何充当序列迭代器的东西(常规指针符合条件)。
std::transform works fine if you have a start-pointer and an end-pointer, too. It works on anything that behaves as sequence iterators (regular pointers qualify).