惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Last Week in AI
Last Week in AI
月光博客
月光博客
D
DataBreaches.Net
WordPress大学
WordPress大学
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
C
Check Point Blog
F
Fortinet All Blogs
B
Blog
小众软件
小众软件
Vercel News
Vercel News
罗磊的独立博客
有赞技术团队
有赞技术团队

博客园 - Lee-hp

SQL:去掉重复行,只保留第一行 游标(转3) 游标(转2) 游标(转) Sql:取每个分类的前10条数据 sql:去掉重复行 水晶报表参考资料 xml操作 页面信息抓取 在GridView中绑定Radio 引用js脚本的奇怪问题 读博 好好工作,好好学习 游标使用——获取每个分类的前10条数据 Url传递中文 SQL触发器--插入时判断数据是否已存在 程序开发和参考资料 SQLServer - 标识列自增 锻炼你的专注力
页面信息抓取(二)
Lee-hp · 2007-04-11 · via 博客园 - Lee-hp

原先提取源代码的方法会出现部分乱码现象,现在用如下方法:
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文件,然后套用母板实现显示。