获取网页的指定内容。

Rojo.Cai 2010-11-22 09:47:14
<H3 class="bold lh20 px14">影片类型: </H3>
<DIV class="line_dot mb6"></DIV>
<UL class=pl28 id=fffff>
<LI><A href="http://movie.mtime.com/movie/section/genre/Adventure/"
target=_blank>冒险</A>   /   <A
href="http://movie.mtime.com/movie/section/genre/Mystery/"
target=_blank>悬疑</A>   /   <A
href="http://movie.mtime.com/movie/section/genre/Action/"
target=_blank>动作</A>   /   <A
href="http://movie.mtime.com/movie/section/genre/Fantasy/"
target=_blank>奇幻</A>   </LI></UL></DIV>
<DIV class="mt30 lh18">
<H3 class="bold lh20 px14">片长:</H3>
<DIV class="line_dot mb6"></DIV>
<UL class=pl28>
<LI>146分钟 </LI></UL></DIV>
<DIV class="mt30 lh18">
<H3 class="bold lh20 px14">国家/地区: </H3>
<DIV class="line_dot mb6"></DIV>
<UL class=pl28>
<LI><A href="http://movie.mtime.com/movie/section/area/UK/"
target=_blank>英国</A>   /   <A
href="http://movie.mtime.com/movie/section/area/USA/"
target=_blank>美国</A>   </LI></UL></DIV>

<H3 class="lh20 px14 mt30" id=Director>导演 Director:</H3>
<DIV class=line_dot></DIV>
<UL class=staff_actor_list>
<LI><A href="http://people.mtime.com/1178899/">大卫·叶茨</A> <A
href="http://people.mtime.com/1178899/">David Yates</A></LI></UL>
<H3 class="lh20 px14 mt30" id=Writer>编剧 Writer:</H3>
<DIV class=line_dot></DIV>
<UL class=staff_actor_list>
<LI><A href="http://people.mtime.com/898487/">斯蒂芬·科洛弗</A> <A
href="http://people.mtime.com/898487/">Steven Kloves</A>  <EM
class="c_a5 ml6">....(screenplay)</EM></LI>
<LI><A href="http://people.mtime.com/902670/">J·K·罗琳</A> <A
href="http://people.mtime.com/902670/">J.K. Rowling</A>  <EM
class="c_a5 ml6">....(novel "Harry Potter and the Deathly
Hallows")</EM></LI></UL>
<H3 class="lh20 px14 mt30" id=Actor>演员 Actor:</H3>
<DIV class=line_dot></DIV>
<UL class=staff_actor_list>
<LI><A href="http://people.mtime.com/915328/"><IMG alt="Daniel Radcliffe"
src="哈利·波特与死亡圣器(上) 演职员表.files/915328_22X22.jpg"></A><A
href="http://people.mtime.com/915328/">丹尼尔·雷德克里夫</A> <A
href="http://people.mtime.com/915328/">Daniel Radcliffe</A>  <EM class="c_a5 ml6">....Harry Potter</EM></LI>

现在想获得
类型:冒险 悬疑 动作 奇幻
国家/地区:英国 美国,
导演:大卫·叶茨 David Yates
编剧:斯蒂芬·科洛弗 Steven Kloves J·K·罗琳 J.K. Rowling
演员:丹尼尔·雷德克里夫 Daniel Radcliffe....Harry Potter







...全文
232 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
俺调试过的。先把分结了,妖哥 俺把另外一帖的分也给你了。不多。20分
http://topic.csdn.net/u/20101120/23/74742a72-95f0-4e3e-939e-18dc7d1d7531.html
dfdscx 2010-11-23
  • 打赏
  • 举报
回复
用正则来过滤应该可以吧
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
妖哥幸苦了。先怕个马屁。等吃完饭,准备葫芦然后画瓢,谢谢。

调试后,在另外开帖,给分
ccrun.com 2010-11-23
  • 打赏
  • 举报
回复
最初的时候考虑用IHTMLDocument2接口来分析页面元素,但是经研究发现,这个网页的元素好多都没有ID或Name,这样定位会困难,所以采用了分析HTML字符串的方式.

不管采用哪种方式实现,如果该页面改版,都要重新分析元素或字符串.

其他的几个页面楼主自己照葫芦画瓢吧.
ccrun.com 2010-11-23
  • 打赏
  • 举报
回复
这些代码的调试花费我太多的时间.

首先在窗体上添加一个IdHTTP组件和一个Memo还有一个Button, 然后为Form添加一个自定义函数:
private:	// User declarations
void __fastcall OutputLog(String strMsg);


在.cpp文件中:
void __fastcall TForm1::OutputLog(String strMsg)
{
Memo1->Lines->Add(FormatDateTime("hh:nn:ss ", Now()) + strMsg);
}


在按钮的点击事件中:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
OutputLog("正在获取网页数据...");

TMemoryStream *ms = new TMemoryStream;
IdHTTP1->Get("http://movie.mtime.com/56856/", ms);
LPSTR lpBuf = new char[ms->Size];
ms->Position = 0;
ms->Read(lpBuf, ms->Size);
delete ms;

String strText = Utf8ToAnsi(AnsiString(lpBuf));
delete []lpBuf;

OutputLog("网页数据获取完毕, 正在分析页面元素...");

String strFind, strTemp;
int nPos;

// 分析 上映日期
strFind = "<strong class=\"bold\">上映日期:</strong>";
nPos = strText.Pos(strFind);

strTemp = strText.SubString(nPos + strFind.Length() + 1, 256);
strTemp = strTemp.SubString(1, strTemp.Pos("<a href") - 1);

OutputLog("上映日期:" + CrnRemoveHTMLTag(strTemp));

// 分析 国家/地区
strText = strText.SubString(nPos, strText.Length());
strFind = "<strong>国家/地区:</strong>";
nPos = strText.Pos(strFind);

strTemp = strText.SubString(nPos + strFind.Length() + 1, 1024);
strTemp = strTemp.SubString(1, strTemp.Pos("</li>") - 1);

OutputLog("国家/地区:" + CrnRemoveHTMLTag(strTemp));

// 分析 类型
strText = strText.SubString(nPos, strText.Length());
strFind = "<strong>类型:</strong>";
nPos = strText.Pos(strFind);

strTemp = strText.SubString(nPos + strFind.Length() + 1, 1024);
strTemp = strTemp.SubString(1, strTemp.Pos("</li>") - 1);

OutputLog("类型:" + CrnRemoveHTMLTag(strTemp));

OutputLog("分析完成.");
}


里面用的自定义函数在这里:
String __fastcall CrnRemoveHTMLTag(String strHtmlText)
{
int nPos;
String strResult;
while (strHtmlText.Pos("<") > 0)
{
nPos = strHtmlText.Pos("<");
strResult += strHtmlText.SubString(1, nPos - 1);
strHtmlText = strHtmlText.Delete(1, strHtmlText.Pos(">"));
}

strResult += strHtmlText;

strResult = StringReplace(strResult.Trim(), "\t", "", TReplaceFlags() << rfReplaceAll);
strResult = StringReplace(strResult, " ", " ", TReplaceFlags() << rfReplaceAll);

// 这一行是根据那个页面多加的,应该是网页制作者的笔误,少写了一个分号
strResult = StringReplace(strResult, " ", " ", TReplaceFlags() << rfReplaceAll);

return strResult;
}


代码在C++Builder2010中编译通过, 运行结果如下:
17:36:08 正在获取网页数据...
17:36:11 网页数据获取完毕, 正在分析页面元素...
17:36:11 上映日期:2010年11月19日 中国
17:36:11 国家/地区:英国 / 美国
17:36:11 类型:冒险 / 悬疑 / 动作 / 奇幻
17:36:11 分析完成.
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
谢谢妖哥。等待
ccrun.com 2010-11-23
  • 打赏
  • 举报
回复
晕..这个要求确实太多了,我帮你写一个例子,你自己举一反三吧.
耐心等待就是了.
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
http://movie.mtime.com/56856/

这个是第一页,我想抓取

上映日期: 2010年11月19日
国家/地区: 英国 / 美国
类型: 冒险 / 悬疑 / 动作 / 奇幻


http://movie.mtime.com/56856/fullcredits.html

这个
我想要的是
导演 编剧 演员
包括 中文,英文名


http://movie.mtime.com/56856/company_credits.html
制作公司:

发行公司:

妖哥,我知道要求多了,到时再开帖加分吧。谢谢了,

ccrun.com 2010-11-23
  • 打赏
  • 举报
回复
你还是把那个页面的地址发出来吧.
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
妖哥,能给个例子吗?
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
妖哥,来给个例子吗?
ccrun.com 2010-11-23
  • 打赏
  • 举报
回复
可以通过IHTMLDocument2接口来分析
_JeffreyWu 2010-11-23
  • 打赏
  • 举报
回复
解析字符串嘛
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
BCB2009
Rojo.Cai 2010-11-23
  • 打赏
  • 举报
回复
可以给个例子参考下吗?

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧