














原先提取源代码的方法会出现部分乱码现象,现在用如下方法:
public string GetSourceHtml(string urlstr)
{
string reshtml = string.Empty;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlstr);
request.Timeout = 300000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader srContent = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
reshtml = srContent.ReadToEnd();
reshtml = reshtml.Replace("\r\n", "");
reshtml = reshtml.Replace("\t", "");
response.Close();
srContent.Close();
}
catch (Exception ex)
{
strError = ex.Message;
}
return reshtml.ToLower();
}
已实现提取相关内容(有一定标记的),具体点讲,是先确定标题的位置,然后提取标题下表格内的内容:
public string GetContent(string reshtml, string title)
{
int bodyNum = reshtml.IndexOf("<body");
//标题的位置
int titleposition = reshtml.IndexOf(title, bodyNum);
//table起始位置
int tablestart = 0;
if (titleposition != -1)
{
tablestart = reshtml.IndexOf("<table", titleposition);
//寻找table的结束位置
int tableend = reshtml.IndexOf("</table>", tablestart) + 8;
//判断是否是结束位置
bool isend = false;
int temp = tablestart;
while (!isend)
{
int xx = reshtml.IndexOf("<table", temp+6, (tableend - temp));
if (xx != -1)
{
temp = tableend;
tableend = reshtml.IndexOf("</table>", tableend) + 8;
isend = false;
}
else
{
isend = true;
}
}
string resultable = reshtml.Substring(tablestart, (tableend - tablestart));
return resultable;
}
else
{
return "";
}
}
但是在将提取内容入库时,有时会遇到如下错误,尚未能解决,达人留步,在此谢过!如下:
"传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。参数 3 (\"@values\"): 数据类型 0xA7 的数据长度或元数据长度无效。
现在改变方法来实现需求的,将提取的内容保存为HTML文件,然后套用母板实现显示。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。