妖哥,不好意思,再麻烦你一下。

Rojo.Cai 2010-11-27 04:42:01
关于那个网页抓取数据的问题
原帖http://topic.csdn.net/u/20101122/21/17e2800a-a02c-4fe4-aaaa-a685aea28340.html

那里是抓取的一部份。

现在网页 换成http://movie.mtime.com/56856/company_credits.html
我想抓取那个制作公司。
网页里面是这样

<H3 class="bold lh20 px14">制作公司:</H3>
<DIV class="line_dot mb6"></DIV>
<OL class="pl28 list_num">
<LI><A href="http://movie.mtime.com/movie/company/25181/">盛日影业公司</A> <SPAN
class=ml6>[英国]</SPAN> <EM class="c_a5 ml6"></EM>
<LI><A href="http://movie.mtime.com/movie/company/11547/">华纳兄弟影片公司</A> <SPAN
class=ml6>[美国]</SPAN> <EM class="c_a5 ml6"></EM></LI></OL></DIV>

-----------------------------------------------------------------------

我就依样画葫芦。

[code=C/C++]
String strUrl = “http://movie.mtime.com/56856/company_credits.html” ;
TMemoryStream *ms = new TMemoryStream;
IdHTTP1->Get(strUrl, ms);
LPSTR lpBuf = new char[ms->Size];
ms->Position = 0;
ms->Read(lpBuf, ms->Size);
delete ms;

String strText = Utf8ToAnsi(AnsiString(lpBuf));
delete []lpBuf;
String strFind, strTemp;
int nPos;

// 分析 制作公司
strFind = "<H3 class=\"bold lh20 px14\">制作公司:</H3>";
nPos = strText.Pos(strFind);

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

OutputLog("制作公司:" + CrnRemoveHTMLTag(strTemp));
MemoCompany->Text = CrnRemoveHTMLTag(strTemp);code]

nPos 抓出来都是 0;
不好意思,依样画葫芦,没画出来。再麻烦一下妖哥了
...全文
202 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccrun.com 2010-11-29
  • 打赏
  • 举报
回复
哪个页面上有制作公司?我似乎没有看到。
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复
"<h3 id=\"Director\" class=\"lh20 px14 mt30\">导演 Director:</h3>";
原来要反一下。
唉。


妖哥
OutputLog("制作公司:" + CrnRemoveHTMLTag(strTemp));
Memo1->Lines->Add(CrnRemoveHTMLTag(strTemp));
如果有多个制作公司的话,,现在是直接接在背后的

盛日影业公司[英国] 华纳兄弟影片公司[美国]

我现在想
memo一个公司一行;
盛日影业公司[英国]
华纳兄弟影片公司[美国]

这个帮忙秀修改下
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复

strFind = "<h3 id=\"Actor\" class=\"lh20 px14 mt30\">演员 Actor:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length(), 8428);
strTemp = strTemp.SubString(1, strTemp.Pos("</li>") - 1);
OutputLog("演员:" + CrnRemoveHTMLTag(strTemp));
MemoActor->Lines->Add(CrnRemoveHTMLTag(strTemp));

strTemp.Pos("</li>") 到第一个("</li>") 就停止掉了,也就是抓到一个演员就停了
然后我把设置为strTemp.Pos("</ul>")或者strTemp.Pos("</em></li></ul>")或者strTemp.Pos("<h3")
演员一个也抓不到,
http://movie.mtime.com/56856/fullcredits.html
网页
<LI><A href="http://people.mtime.com/1273637/"><IMG alt=""
src="哈利·波特与死亡圣器(上) 演职员表.files/1273637_22X22.jpg"></A><A
href="http://people.mtime.com/1273637/">Granville Saxton</A>  <EM
class="c_a5 ml6">....Death Eater</EM></LI></UL><H3 class="lh20 px14 mt30" id="Produced by">制作人 Produced by:</H3>
<DIV class=line_dot></DIV>
<UL class=staff_actor_list>
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 ccrun 的回复:]
这一行代码和404的错误没有任何联系.

检查IdHTTP1->Get的网址.
[/Quote]
真不好意思,,是网址出现错误了,,少了个“/”
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复

OutputLog("正在获取网页数据...");
String strUrl = “http://movie.mtime.com/56856” ;
TMemoryStream *ms = new TMemoryStream;
IdHTTP1->Get(strUrl, 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));
MovieTime->Text = 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));
MovieCountry->Text = 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));
//---------------------------------------------------------------------------
strUrl = “http://movie.mtime.com/56856/company_credits.html” ;
TMemoryStream *ms2 = new TMemoryStream;
IdHTTP1->Get(strUrl, ms2);
lpBuf = new char[ms2->Size];
ms2->Position = 0;
ms2->Read(lpBuf, ms2->Size);
delete ms2;

strText = Utf8ToAnsi(AnsiString(lpBuf));
delete []lpBuf;
// 制作公司
strFind = "<h3 class=\"bold lh20 px14\">制作公司:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length() + 1, 2048);
strTemp = strTemp.SubString(1, strTemp.Pos("</h3>") - 1);
OutputLog("制作公司:" + CrnRemoveHTMLTag(strTemp));
//发行公司
strText = strText.SubString(nPos, strText.Length());
strFind = "<h3 class=\"bold lh20 px14\">发行公司:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length() + 1, 2048);
strTemp = strTemp.SubString(1, strTemp.Pos("</h3>") - 1);
OutputLog("发行公司:" + CrnRemoveHTMLTag(strTemp));
//--------------------------------------------------------------------
strUrl = “http://movie.mtime.com/56856/fullcredits.html” ;
TMemoryStream *ms3 = new TMemoryStream;
IdHTTP1->Get(strUrl, ms3);
lpBuf = new char[ms3->Size];
ms3->Position = 0;
ms3->Read(lpBuf, ms3->Size);
delete ms3;
strText = Utf8ToAnsi(AnsiString(lpBuf));
delete []lpBuf;
// 导演
strFind ="<h3 id=\"Director\" class=\"lh20 px14 mt30\">导演 Director:</h3>";
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 = "<h3 id=\"Writer\" class=\"lh20 px14 mt30\">编剧 Writer:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length() + 1, 2048);
strTemp = strTemp.SubString(1, strTemp.Pos("</li>") - 1);
OutputLog("编剧:" + CrnRemoveHTMLTag(strTemp));
//演员
strFind = "<h3 id=\"Writer\" class=\"lh20 px14 mt30\">演员 Actor:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length() + 1, 3072);
strTemp = strTemp.SubString(1, strTemp.Pos("</li>") - 1);
OutputLog("演员:" + CrnRemoveHTMLTag(strTemp));
OutputLog("分析完成.");

编译出现 HTTP/1.1 404 NOT FOUND
wolfmvp 2010-11-29
  • 打赏
  • 举报
回复
向妖哥,学习!向妖哥,致敬!专注…
ccrun.com 2010-11-29
  • 打赏
  • 举报
回复
这一行代码和404的错误没有任何联系.

检查IdHTTP1->Get的网址.
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复
String __fastcall CrnRemoveHTMLTag(String strHtmlText)

原来的

String __fastcall CrnRemoveHTMLTag2(String strHtmlText)

// 多了这么一句,但是可能会影响到分析其他的页面.
strHtmlText = StringReplace(strHtmlText, "</li>", "\r\n", TReplaceFlags() << rfReplaceAll);

+
原来的


我在制作公司 用 CrnRemoveHTMLTag2
结果出现上述错误了
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复
编译出现 HTTP/1.1 404 NOT FOUND.这是为什么
ccrun.com 2010-11-29
  • 打赏
  • 举报
回复
因为前面"编剧"这一段截取的字符串较长, 把演员部分的字符串也截取走了, 所以导致Pos函数没找到"演员 Actor"这几个字符.


//演员
下面这一行删掉:
strText = strText.SubString(nPos, strText.Length());
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复

// 导演
strFind = "<h3 id=\"Director\" class=\"lh20 px14 mt30\">导演 Director:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length(), 256);
strTemp = strTemp.SubString(1, strTemp.Pos("</li>") - 1);
OutputLog("导演:" + CrnRemoveHTMLTag(strTemp));
MemoDirector->Lines->Add(CrnRemoveHTMLTag(strTemp));

//编剧
strText = strText.SubString(nPos, strText.Length());
strFind = "<h3 id=\"Writer\" class=\"lh20 px14 mt30\">编剧 Writer:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length(), 1048);
strTemp = strTemp.SubString(1, strTemp.Pos("</ul>") - 1);
OutputLog("编剧:" + CrnRemoveHTMLTag(strTemp));
MemoWriter->Lines->Add(CrnRemoveHTMLTag(strTemp));

//演员
strText = strText.SubString(nPos, strText.Length());
strFind = "<h3 id=\"Actor\" class=\"lh20 px14 mt30\">演员 Actor:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length(), 8428);
strTemp = strTemp.SubString(1, strTemp.Pos("</em></li></ul>") - 1);
OutputLog("演员:" + CrnRemoveHTMLTag(strTemp));
MemoActor->Lines->Add(CrnRemoveHTMLTag(strTemp));

http://movie.mtime.com/56856/fullcredits.html
妖哥 在抓演员那里
strTemp.Pos("")应该设置那里合适啊。
ccrun.com 2010-11-29
  • 打赏
  • 举报
回复
仔细看代码中的变化
String __fastcall CrnRemoveHTMLTag(String strHtmlText)
{
// 多了这么一句,但是可能会影响到分析其他的页面.
strHtmlText = StringReplace(strHtmlText, "</li>", "\r\n", TReplaceFlags() << rfReplaceAll);

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;
}

// ---------------------------------------------------------------------------
//
void __fastcall TForm1::Button1Click(TObject *Sender)
{
OutputLog("正在获取网页数据...");

TMemoryStream *ms = new TMemoryStream;
IdHTTP1->Get("http://movie.mtime.com/56856/company_credits.html", 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 = "<h3 class=\"bold lh20 px14\">制作公司:</h3>";
nPos = strText.Pos(strFind);

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

OutputLog("制作公司:" + CrnRemoveHTMLTag(strTemp));

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


输出:
16:29:33 正在获取网页数据...
16:29:35 网页数据获取完毕, 正在分析页面元素...
16:29:35 制作公司:盛日影业公司[英国]
华纳兄弟影片公司[美国]
16:29:35 分析完成.
Rojo.Cai 2010-11-29
  • 打赏
  • 举报
回复
在这里 String strUrl = “http://movie.mtime.com/56856/company_credits.html”

strFind = "<h3 class=\"bold lh20 px14\">制作公司:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length() + 1, 1024);
strTemp = strTemp.SubString(1, strTemp.Pos("</ol>") - 1);
OutputLog("制作公司:" + CrnRemoveHTMLTag(strTemp));
MemoCompany->Text = CrnRemoveHTMLTag2(strTemp);

抓出来是 2个 盛日影业公司[英国] 华纳兄弟影片公司[美国]
现在就是想
在Memo里变成 一行一个
盛日影业公司[英国]
华纳兄弟影片公司[美国]

同样在这个页面 发行公司 也很多,,
同理在http://movie.mtime.com/56856/fullcredits.html
演员也是很多个,
如果一个接在一个后面感觉很难看。
所以。。。



ccrun.com 2010-11-28
  • 打赏
  • 举报
回复
由于每个页面中文字内容的不同,建议每次SubString时指定的长度大一些,比如1024或2048以上。
ccrun.com 2010-11-28
  • 打赏
  • 举报
回复
获取导演:
OutputLog("正在获取网页数据...");

TMemoryStream *ms = new TMemoryStream;
IdHTTP1->Get("http://movie.mtime.com/56856/fullcredits.html", 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 = "<h3 id=\"Director\" class=\"lh20 px14 mt30\">导演 Director:</h3>";
nPos = strText.Pos(strFind);

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

OutputLog("导演:" + CrnRemoveHTMLTag(strTemp));

OutputLog("分析完成.");


输出:
23:14:22 正在获取网页数据...
23:14:23 网页数据获取完毕, 正在分析页面元素...
23:14:23 导演:大卫·叶茨 David Yates
23:14:23 分析完成.
ccrun.com 2010-11-28
  • 打赏
  • 举报
回复
Rojo.Cai 2010-11-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ccrun 的回复:]
strTemp = strText.SubString(nPos + strFind.Length() + 1, 256);
这一行加个断点,看看nPos的值是多少。如果是0,说明strText中没有找到strFind那个字符串。
[/Quote]
nPos 一直是 0 ;
Rojo.Cai 2010-11-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ccrun 的回复:]
strTemp = strText.SubString(nPos + strFind.Length() + 1, 256);
这一行加个断点,看看nPos的值是多少。如果是0,说明strText中没有找到strFind那个字符串。

另外 “抓一个换一行”我不知道你要的是什么效果。
[/Quote]


妖哥,在
OutputLog("制作公司:" + CrnRemoveHTMLTag(strTemp));
Memo1->Lines->Add(CrnRemoveHTMLTag(strTemp));
如果有多个制作公司的话,,现在是直接接在背后的

盛日影业公司[英国] 华纳兄弟影片公司[美国]

我现在想
memo一个公司一行;
盛日影业公司[英国]
华纳兄弟影片公司[美国]
ccrun.com 2010-11-28
  • 打赏
  • 举报
回复
strTemp = strText.SubString(nPos + strFind.Length() + 1, 256);
这一行加个断点,看看nPos的值是多少。如果是0,说明strText中没有找到strFind那个字符串。

另外 “抓一个换一行”我不知道你要的是什么效果。
Rojo.Cai 2010-11-28
  • 打赏
  • 举报
回复
一直在火车上,现在才看到。
妖哥,刚试了下。

还有个问题 http://movie.mtime.com/56856/fullcredits.html

<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>

    strFind = "<h3 class=\"lh20 px14 mt30\" id=Director>导演 Director:</h3>";
nPos = strText.Pos(strFind);
strTemp = strText.SubString(nPos + strFind.Length() + 1, 256);
strTemp = strTemp.SubString(1, strTemp.Pos("</li>") - 1);


导演那里 妖哥你慧眼看下,错误在那里。

CrnRemoveHTMLTag(strTemp));
这里想再请妖哥,数据 抓一个换一行。

加载更多回复(6)

703

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder ActiveX/COM/DCOM
社区管理员
  • ActiveX/COM/DCOM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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