procedure TGetThread.Execute;var i, k: integer; q1, q2, q3, q4: integer; tmpHtml: WideString; PicUrl: string; tmpStream: TStringStream; //TMemoryStream不能输出成String CPUUsage: Double;begin SearchKeyWordListTH:=THashedStringList.Create; //关键字考虑优先顺序 //SearchKeyWordList.Sorted:=true; //SearchKeyWordList.Duplicates:=dupIgnore; //Sorted=true时起作用 SearchKeyWordListTH.Clear; if (SearchKeyWordsTH<>'') then ExtractStrings([';'], [' '], PChar(SearchKeyWordsTH), SearchKeyWordListTH); // FindLinkListTH:=THashedStringList.Create; FindLinkListTH.Sorted:=true; FindLinkListTH.Duplicates:=dupIgnore; //Sorted=true时起作用 FindPicListTH:=THashedStringList.Create; FindPicListTH.Sorted:=true; FindPicListTH.Duplicates:=dupIgnore; //Sorted=true时起作用 SucceedDownloadPicListTH:=THashedStringList.Create; SucceedDownloadPicListTH.Sorted:=true; SucceedDownloadPicListTH.Duplicates:=dupIgnore; //Sorted=true时起作用 FailedDownloadPicListTH:=THashedStringList.Create; FailedDownloadPicListTH.Sorted:=true; FailedDownloadPicListTH.Duplicates:=dupIgnore; //Sorted=true时起作用 // tmpHtml:=''; tmpStream:=TStringStream.Create(''); try while not Terminated do begin Sleep(SleepTime); Application.ProcessMessages; CPUUsage:=-1; if not GetCPUUsage(CPUUsage) then continue; if (CPUUsage<>-1) and (CPUUsage>=MaxCPUUsage) then continue; //取得需要处理的链接 WebPageUrl:=''; Synchronize(GetProcessLink); if WebPageUrl='' then continue; StatusStr:='Prepared to deal with WebPage:'+WebPageUrl; //准备处理网页 Synchronize(ShowStatusInfo); //Sleep(SleepTime); Application.ProcessMessages; if Terminated then break; IDP:=TIdHttp.Create(nil); //不每次重新创建,长时间运行后会Get不到网页 IDP.Request.UserAgent:=RequestUserAgent; IDP.Request.CacheControl:='no-cache'; IDP.HandleRedirects:=true; //IDP.Request.AcceptCharSet AcceptEncoding AcceptLanguage //接受任何的,不理会乱码 //IDP.Request.Clear; //不起作用 //IDP.Response.Clear; try //读取网页 tmpHtml:=''; tmpStream.WriteString(''); try IDP.Get(WebPageUrl, tmpStream); tmpStream.Position:=0; //必须要 tmpHtml:=tmpStream.ReadString(tmpStream.Size); //tmpHtml:=IDP.Get(WebPageUrl); //Utf8ToAnsi或AnsiToUtf8无效 tmpHtml:=LowerCase(tmpHtml); //转成小写,以便后续的网页过滤 //if trim(frmMain.memTest.Text)='' then // frmMain.memTest.Text:=tmpHtml; if Terminated then break; if IsAvaiWebPage(tmpHtml, SearchKeyWordListTH) then begin Synchronize(UpdateSucceedVisitLinkList); end else begin Synchronize(UpdateFailedVisitLinkList); StatusStr:='WebPage do not meet the conditions:'+WebPageUrl; //网页不符合条件 Synchronize(ShowStatusInfo); continue; end; except Synchronize(UpdateFailedVisitLinkList); StatusStr:='Failure to get the WebPage:'+WebPageUrl; //读取网页失败 Synchronize(ShowStatusInfo); continue; end; //Sleep(SleepTime); Application.ProcessMessages; if Terminated then break; //解释获得网页所有链接 if (MaxProcessLinkCountTH=0) or (CurProcessLinkCountTH<MaxProcessLinkCountTH) then begin FindLinkListTH.Clear; GetWebPageUrl(utLinkUrl, tmpHtml, WebPageUrl, FindLinkListTH); q1:=FindLinkListTH.Count; if Terminated then break; Synchronize(DelReElemOfFindLinkList); q2:=FindLinkListTH.Count; if Terminated then break; Synchronize(UpdateProcessLinkList); if Terminated then break; StatusStr:='WebPage:'+WebPageUrl+' Found '+IntToStr(q1)+' Links,Effective '+IntToStr(q2)+' Links. '; //找到 ? 个链接, 有效 ? 个链接 Synchronize(ShowStatusInfo); //Sleep(SleepTime); Application.ProcessMessages; end; if Terminated then break; //解释获得网页所有图片 FindPicListTH.Clear; GetWebPageUrl(utPicUrl, tmpHtml, WebPageUrl, FindPicListTH); q1:=FindPicListTH.Count; if Terminated then break; Synchronize(DelReElemOfFindPicList); q2:=FindPicListTH.Count; if Terminated then break; Synchronize(GetSaveFileNamePart); k:=0; SucceedDownloadPicListTH.Clear; FailedDownloadPicListTH.Clear; if not SaveFile(FindPicListTH, SavePathTH, SaveFileNamePrefix, MinFileSizeTH, SucceedDownloadPicListTH, FailedDownloadPicListTH, k) then ; q2:=q2-k; q3:=SucceedDownloadPicListTH.Count; q4:=FailedDownloadPicListTH.Count-k; if Terminated then break; Synchronize(UpdateSucceedDownloadPicList); if Terminated then break; Synchronize(UpdateFailedDownloadPicList); if Terminated then break; StatusStr:='WebPage processing completed:'+WebPageUrl //网页处理完成 +' Found '+IntToStr(q1)+' Pictures,Effective '+IntToStr(q2)+' Pictures,' //找到 ? 张图片, 有效 ? 张图片 +' Download success '+IntToStr(q3)+' Pictures,' //成功下载 ? 张 +' Download failure '+IntToStr(q4)+' Pictures.'; //失败下载 ? 张 Synchronize(ShowStatusInfo); //Sleep(SleepTime); Application.ProcessMessages; finally FreeAndNil(IDP); end; end; //while finally tmpStream.Free; FreeAndNil(SearchKeyWordListTH); FreeAndNil(FindPicListTH); FreeAndNil(SucceedDownloadPicListTH); FreeAndNil(FailedDownloadPicListTH); FreeAndNil(FindLinkListTH); end; Synchronize(NotifyWorkThreadTerminated);end;