原文:使用HtmlAgilityPack批量抓取网页数据

相关软件点击下载

登录的处理。因为有些网页数据需要登陆后才能提取。这里要使用ieHTTPHeaders来提取登录时的提交信息。

抓取网页

 HtmlAgilityPack.HtmlDocument htmlDoc;

            if (!string.IsNullOrEmpty(登录URL))
            {
                htmlDoc = htmlWeb.Load(登录URL, 提交的用户验证信息, 获取数据的网页URL);
            }
            else
            {
                htmlDoc = htmlWeb.Load(获取数据的网页URL);
            }
        
 ArrayList list = new ArrayList();
            list.add("//table/tr[1]/td");
            list.add("//table/tr[2]/td");
            //获取循环的节点的xpath,比如://table/tr
            HtmlNodeCollection repeatNodes = htmlDoc.DocumentNode.SelectNodes("//table/tr");

            //循环节点
            foreach (HtmlNode node in repeatNodes)
            {
                //循环获取数据
                foreach (string dataPath in list)
                {

                    HtmlNode dataNode = node.SelectSingleNode(list);
                    if (dataNode != null)
                    {
                        string text = dataNode.InnerText;
                    }

                }
            }

如果出现乱码,调整编码集为gb2312或者是utf-8

htmlWeb.DefaultEncoding = System.Text.Encoding.GetEncoding(strEncode);

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

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TestTools.WebTesting;

using HtmlAgilityPack;

public class WebTest1Coded : WebTest

{

public override IEnumerator<WebTestRequest> GetRequestEnumerator()

{

WebTestRequest request1 = new WebTestRequest("http://www.microsoft.com/");

request1.ValidateResponse += new EventHandler<ValidationEventArgs>(request1_ValidateResponse);

yield return request1;

}

void request1_ValidateResponse(object sender, ValidationEventArgs e)

{

//load the response body string as an HtmlAgilityPack.HtmlDocument

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

doc.LoadHtml(e.Response.BodyString);

//locate the "Nav" element

HtmlNode navNode = doc.GetElementbyId("Nav");

//pick the first <li> element

HtmlNode firstNavItemNode = navNode.SelectSingleNode(".//li");

//validate the first list item in the Nav element says "Windows"

e.IsValid = firstNavItemNode.InnerText == "Windows";

}

}

使用HtmlAgilityPack批量抓取网页数据的更多相关文章

  1. java抓取网页数据,登录之后抓取数据。

    最近做了一个从网络上抓取数据的一个小程序.主要关于信贷方面,收集的一些黑名单网站,从该网站上抓取到自己系统中. 也找了一些资料,觉得没有一个很好的,全面的例子.因此在这里做个笔记提醒自己. 首先需要一 ...

  2. Asp.net 使用正则和网络编程抓取网页数据(有用)

    Asp.net 使用正则和网络编程抓取网页数据(有用) Asp.net 使用正则和网络编程抓取网页数据(有用) /// <summary> /// 抓取网页对应内容 /// </su ...

  3. web scraper 抓取网页数据的几个常见问题

    如果你想抓取数据,又懒得写代码了,可以试试 web scraper 抓取数据. 相关文章: 最简单的数据抓取教程,人人都用得上 web scraper 进阶教程,人人都用得上 如果你在使用 web s ...

  4. c#抓取网页数据

    写了一个简单的抓取网页数据的小例子,代码如下: //根据Url地址得到网页的html源码 private string GetWebContent(string Url) { string strRe ...

  5. 使用JAVA抓取网页数据

    一.使用 HttpClient 抓取网页数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  6. 【iOS】正則表達式抓取网页数据制作小词典

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xn4545945/article/details/37684127 应用程序不一定要自己去提供数据. ...

  7. 01 UIPath抓取网页数据并导出Excel(非Table表单)

    上次转载了一篇<UIPath抓取网页数据并导出Excel>的文章,因为那个导出的是table标签中的数据,所以相对比较简单.现实的网页中,有许多不是通过table标签展示的,那又该如何处理 ...

  8. 使用HtmlAgilityPack和ScrapySharp抓取网页数据遇到的几个问题解决方法——格式编码问题

    需要用到对应市区县街道居委会的区域编码,于是找到统计局的网页,对这些数据进行抓取,用到了HtmlAgilityPack和ScrapySharp,由于也是第一次从网页抓取数据,所以对于HtmlAgili ...

  9. Node.js的学习--使用cheerio抓取网页数据

    打算要写一个公开课网站,缺少数据,就决定去网易公开课去抓取一些数据. 前一阵子看过一段时间的Node.js,而且Node.js也比较适合做这个事情,就打算用Node.js去抓取数据. 关键是抓取到网页 ...

随机推荐

  1. ios9 之后 配置百度地图出现的错误

    链接地址:http://blog.csdn.net/u011303663/article/details/48596315 错误 ld: '/Users/xxx/Documents/xxx/Baidu ...

  2. [Oracle]TRIGGER

    题外话: Oracle 的使用. 以前客户方用的是SQL SERVER,一直在用SQL SERVER,感觉SQL SERVER的用户体验非常好. 不管是开发环境的界面布局到SQL 的写法上,感觉写起来 ...

  3. 《C/C++学习指南》 - 关于本书

    称号 :<C/C++学习指南> 笔者: 邵制成 在线学习(免费),在线答疑 (免费) 创作目的:想以自己的能力和经验为国内(及国外)的读者奉献一本好书,一本让人一看就懂."不可能 ...

  4. 微软vs2015先行,Visual Studio 2015正式版离线iso及在线下载(附key)附带百度云盘地址

    win10正式版发布之前我们迎来了vs2015正式版,迫不及待要下载朋友可以看看 Visual Studio Community 2015简体中文版(社区版,针对个人免费): 在线安装exe:http ...

  5. Appium 的安装启动

    Appium 的安装方式根据官网提供的是使用nodejs 安装,但是官方给出了三种方法 参考网址: appium下载地址: https://bitbucket.org/appium/appium.ap ...

  6. Taxonomy of class loader problems encountered when using Jakarta Commons Logging(转)

    Acknowledgments I would like to thank Jacob Kjome for reviewing early drafts of this document. His c ...

  7. Js版游戏打砖块开发过程详细

    最近对js的小游戏开发来了兴趣,前段时间由于回答度娘知道的提问写了个贪吃蛇,虽然难度不大并不复杂,感觉还挺有意思.感觉小时候玩过的什么俄罗斯方块,坦克大战什么的都可以试着用js实现下,这天来了兴致又想 ...

  8. 简化ui文件转换写法

    在命令行敲一串长的命令.枯燥麻烦. #coding:utf-8 import sys import os import subprocess if len(sys.argv) == 2: #节省输入, ...

  9. 关于VC6编译LPDIRECT3D8不通过的问题

    重装了系统,打开以前的工程看看,重新编译发现类似的错误: error C2146: syntax error : missing ';' before identifier 'lpdi' fatal ...

  10. 慎用MonthsBetweent和MonthSpan

    ●function MonthsBetween(const ANow, AThen: TDateTime): Integer; 描述: 根据两个给定的TDateTime类型的参数ANow和AThen, ...