faunus 发表于 2008-11-9 13:20:57

屏幕抓取(screen scraping ) c#版本

Weather Screen Scraping with C#
http://www.csharphelp.com/archives2/archive435.html

HTML Screen Scraping using C# .Net WebClient
http://www.codersource.net/csharp_screen_scraping.html

Screen Scraping
http://www.devcity.net/Articles/48/1/screen_scrape.aspx

Screen Scraping in ASP.NET
http://www.csharpfriends.com/Articles/getTip.aspx?articleID=210

HOW TO: Implement Screen Scraping with ASP.NET
http://www.aspnetworld.com/articles/2004090801.aspx

Screen   Scrapes   in   ASP.NET   
http://www.4guysfromrolla.com/webtech/070601-1.shtml   
   
Screen   Scraping   in   ASP.NET   by:   Salman   
http://www.csharpfriends.com/Articles/getTip.aspx?articleID=210

[ 本帖最后由 faunus 于 2008-11-10 17:38 编辑 ]

faunus 发表于 2008-11-9 13:24:17

用XPath进行Screen Scraping

****


                  Screen Scraping就是从HTML代码中抽取需要的网页信息,因为HTML代码是为人类用户从屏幕上浏览的而不是直接给计算机程序使用的,所以不可避免地需要人工去解析它的提取模式。目前为止自己知道的有三种方式:
         
         1。当然是“正则表达式”,但它把本来有着丰富层次化结构信息的网页当成一个流式的字符串对待,提取模式之复杂可以想象,另一个坏处是它对网页格式变化的极度敏感性;

         2。第二种方式是通过像NekoHTML这样的把HTML转换成标准XML格式的工具,利用DOM树解析的办法提取,这种方式充分利用了网页固有的内在结构,但编码效率依然低下而且很容易出现运行期的空指针异常;

    3。第三种也是目前自己发现的最好的方法是利用XPath语言从HTML对应的XMLDOM树结构中提取,它无需编写遍历树形的代码,仅需一个极为简洁的字符串即可。另外,还可以借助于FireFox中的DomInspector和XPather插件,很容易发现这样极为健壮的XPath字符串。比如想要提取自己博客中文章的标题,如下一条XPath语句再借助于某种XPath类包即可自动抽取出来: //*[@class='tit']/A/text()

       下面给出一段从网页中提取自己想要的链接的简单程序,它需要nekohtml.jar, xercesImpl.jar, xalan.jar等软件包:

            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Please input the URL: ");
            String url = stdin.readLine();
            System.out.println("Please input the charset(default is GB2312): ");
            String charset = stdin.readLine();
            if (charset.trim().length() == 0) {
                     charset = "GB2312";
                     System.out.println("Assuming default charset: GB2312");
            }
            
            HttpClient userAgent = new HttpClient();
            
            DOMParser domParser = new DOMParser();
            
            Document dom = null;
            HttpMethod method = new GetMethod(url);
            
            System.out.println("Start downloading...");
            userAgent.executeMethod(method);
            System.out.println("Done!");
            
            if (method.getStatusCode() == HttpStatus.SC_OK) {
                     InputSource source = new InputSource(method
                                          .getResponseBodyAsStream());
                     source.setEncoding(charset);
                     // NekoHTML没有实现XML的namespace功能
                     // 见http://people.apache.org/~andyc/neko/doc/html/faq.html#hierarchy
                     domParser.setFeature("http://xml.org/sax/features/namespaces", false);
                     
                     domParser.parse(source);
                     dom = domParser.getDocument();
                     if (dom != null)
                            dom.normalize();
                     
                     Node rootNode = dom.getDocumentElement();
                     
                     System.out.println("The DOM tree has been produced successfully.");
                     
                     String xpathForAnchor = null;
                     
                     String yesOrNo = "y";
                     
                     List<String> history = new ArrayList<String>();
                     
                     NodeList anchorNodes = null;
                     
                     
                     do {
                            if (history.size() > 0) {
                                 System.out.println("============");
                                 System.out.println("history:");
                                 for (String item : history) {
                                          System.out.println("\t" + item);
                                 }
                                 System.out.println("============");
                            }
                            System.out.println("Please input your XPath for Anchor:");
                            xpathForAnchor = stdin.readLine();
                           
                            history.add(xpathForAnchor);
                           
                            try {
                                 anchorNodes = XPathAPI.selectNodeList(rootNode, xpathForAnchor);
                            } catch (TransformerException e) {
                                 System.out.println("Your xpath is not valid, sorry");
                                 continue;
                            }

                            for (int i = 0; i < anchorNodes.getLength(); i++) {
                                 Node anchorNode = anchorNodes.item(i);
                                 StringBuilder sb = new StringBuilder();
                                 sb.append(String.format("<item>%n"));
                                 sb.append(String.format("\t<text>%s</text>%n", anchorNode.getTextContent()));
                                 sb.append(String.format("</item>%n"));
                                 System.out.println(sb.toString());
                            }
                           
                            System.out.println("Are you satisfied with the result(y/n) ? ");
                           
                            yesOrNo = stdin.readLine();
                           
                     } while (!yesOrNo.startsWith("y"));

faunus 发表于 2008-11-9 13:28:49

Screen Scraping和Web Scraping的区别

****

      Screen Scraping和Web Scraping的区别并没有一个权威的说明,
假设我们将Screen Scraping理解成用一个充当浏览器的程序提取信息,
而将Web Scraping理解成用一个不具有浏览器功能的程序提取信息。

      ScreenScraping类的最主要特点就是有一个标准的Web浏览器
引擎,能够支持所有标准的Web技术,信息提取程序以一种所见即所
得的方式将数据提取下来。例如,在HTML源代码中用<br>表示换行,
ScreenScraping类并不关心HTML源代码是怎样的,它看到的是已经
被浏览器引擎解释过的内容,即看到了一个换行符0x0a0x0d,提取的
结果中也会包含这个控制字符。

      WebScraping类的实现有多种方法,例如,使用正则表达式处理,
对下载下来的HTML文档进行正则表达式匹配,以找到目标数据。可见,
它所见到的是HTML文档源代码,同样举上一个例子,它能够很容易的
提取到字符串<br>,而不是控制符0x0a0x0d。又如,使用一些开发平
台提供的HTML DOM程序库,这种方法与ScreenScraping很类似,但
是其能力仍然无法与浏览器匹敌,例如,将JavaScript引擎集成进来是
个难点。

faunus 发表于 2008-11-9 14:44:38

How Web Scraping Works

Web Scraping is essentially reverse engineering of HTML pages. Itcan also be thought of as parsing out chunks of information from apage. Web pages are coded in HTML, which uses a tree-like structure torepresent the information. The actual data is mingled with layout andrendering information and is not readily available to a computer.Scrapers are the programs that “know” how to get the data back from agiven HTML page. They work by learning the details of the particularmarkup and figuring out where the actual data is. For example, in theillustration below the scraper extracts URLs from the del.icio.us page.By applying such a scraper, it is possible to discover what URLs aretagged with any given tag.

页面抓取本质上是HTML页面的反向工程,也可以看成页面解释器,网页以HTML编码,HTML以树型结构表示信息,实际数据与布局代码以及效果信息混杂在一起,不能被计算机直接利用。抓取器程序“知道”怎样从给定HTML页面中抓取数据。它们通过分析网页特定的标注方式找到实际数据,我们可以找到被任意标签标记的链接。

faunus 发表于 2008-11-14 20:40:49

蛙蛙推荐:蛙蛙牌网页捕捉器(*****)
http://www.cnblogs.com/onlytiancai/archive/2008/10/22/cap_ie.html

蛙蛙推荐:蛙蛙牌firefox插件(*****)
http://www.cnblogs.com/onlytiancai/archive/2008/10/22/cap_ie.html

转贴:用C#下的Raw Socket编程实现网络封包监视,来自网络,来源不祥
http://www.cnblogs.com/onlytiancai/archive/2007/10/14/924075.html

自己做一个抓包工具(类库是用的别人的)
http://www.cnblogs.com/onlytiancai/archive/2007/10/14/924103.html

蛙蛙浏览器
http://www.cnblogs.com/onlytiancai/archive/2008/09/15/wawa_application_browser.html

[ 本帖最后由 faunus 于 2008-11-14 20:56 编辑 ]

lbjyuer 发表于 2016-2-10 00:03:15

和你们一块学习。。。
页: [1]
查看完整版本: 屏幕抓取(screen scraping ) c#版本