.net抓取网页数据
1、想通过代码获得某个页面的数据,首先根据右键查看页面源代码,通过分析。再通过下面代码,修改,一步步查找出所需内容,存入数据库。
//根据Url地址得到网页的html源码
private string GetWebContent(string Url)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//声明一个HttpWebRequest请求
request.Timeout = ;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("utf-8");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
strResult = streamReader.ReadToEnd();
}
catch
{
MessageBox.Show("出错");
}
return strResult;
} private void button1_Click(object sender, EventArgs e)
{
//要抓取的URL地址
string Url = "http://kxt.com/data/20.html"; //☆☆☆☆☆☆☆☆☆☆
//得到指定Url的源码
string strWebContent = GetWebContent(Url);
richTextBox1.Text = strWebContent;
//取出和数据有关的那段源码
int iBodyStart = strWebContent.IndexOf("<body", );
int iStart = strWebContent.IndexOf("历史数据", iBodyStart);
int iTableStart = strWebContent.IndexOf("<ul", iStart);
int iTableEnd = strWebContent.IndexOf("</ul>", iTableStart);
string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart + );
//生成HtmlDocument
WebBrowser webb = new WebBrowser();
webb.Navigate("about:blank");
HtmlDocument htmldoc = webb.Document.OpenNew(true);
htmldoc.Write(strWeb);
HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("li");
int i = ;
foreach (HtmlElement tr in htmlTR)
{
i++;
if (i == )
{
continue;
}
if (i == htmlTR.Count - )
{
break;
}
HtmlElementCollection spans = tr.GetElementsByTagName("span"); string dateTime = spans[].InnerText; string netWeightOunce = spans[].InnerText;
string netWeightTon = spans[].InnerText;
string totalValue = spans[].InnerText;
string regulation = spans[].InnerText;
//string affectOil = spans[5].InnerText; //Id, UpdateTime, NetWeightOunce, NetWeightTon, TotalValue, Regulation, FinanceTime
SqlServer ado=new SqlServer();
ado.AddField("UpdateTime",DateTime.Now);
ado.AddField("NetWeightOunce", netWeightOunce);
ado.AddField("NetWeightTon", netWeightTon);
ado.AddField("TotalValue", totalValue);
// ado.AddField("EffectOil", affectOil);
ado.AddField("Regulation", regulation);
ado.AddField("FinanceTime", Convert.ToDateTime(dateTime).ToString("yyyy-MM-dd"));//☆☆☆☆☆☆☆☆☆☆ ado.Insert("Silver");//☆☆☆☆☆☆☆☆☆☆ } MessageBox.Show("OK"); }
.net抓取网页数据的更多相关文章
- java抓取网页数据,登录之后抓取数据。
最近做了一个从网络上抓取数据的一个小程序.主要关于信贷方面,收集的一些黑名单网站,从该网站上抓取到自己系统中. 也找了一些资料,觉得没有一个很好的,全面的例子.因此在这里做个笔记提醒自己. 首先需要一 ...
- Asp.net 使用正则和网络编程抓取网页数据(有用)
Asp.net 使用正则和网络编程抓取网页数据(有用) Asp.net 使用正则和网络编程抓取网页数据(有用) /// <summary> /// 抓取网页对应内容 /// </su ...
- 使用HtmlAgilityPack批量抓取网页数据
原文:使用HtmlAgilityPack批量抓取网页数据 相关软件点击下载登录的处理.因为有些网页数据需要登陆后才能提取.这里要使用ieHTTPHeaders来提取登录时的提交信息.抓取网页 Htm ...
- web scraper 抓取网页数据的几个常见问题
如果你想抓取数据,又懒得写代码了,可以试试 web scraper 抓取数据. 相关文章: 最简单的数据抓取教程,人人都用得上 web scraper 进阶教程,人人都用得上 如果你在使用 web s ...
- c#抓取网页数据
写了一个简单的抓取网页数据的小例子,代码如下: //根据Url地址得到网页的html源码 private string GetWebContent(string Url) { string strRe ...
- 使用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 ...
- 【iOS】正則表達式抓取网页数据制作小词典
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xn4545945/article/details/37684127 应用程序不一定要自己去提供数据. ...
- 01 UIPath抓取网页数据并导出Excel(非Table表单)
上次转载了一篇<UIPath抓取网页数据并导出Excel>的文章,因为那个导出的是table标签中的数据,所以相对比较简单.现实的网页中,有许多不是通过table标签展示的,那又该如何处理 ...
- Node.js的学习--使用cheerio抓取网页数据
打算要写一个公开课网站,缺少数据,就决定去网易公开课去抓取一些数据. 前一阵子看过一段时间的Node.js,而且Node.js也比较适合做这个事情,就打算用Node.js去抓取数据. 关键是抓取到网页 ...
- Java抓取网页数据(原网页+Javascript返回数据)
有时候由于种种原因,我们需要采集某个网站的数据,但由于不同网站对数据的显示方式略有不同! 本文就用Java给大家演示如何抓取网站的数据:(1)抓取原网页数据:(2)抓取网页Javascript返回的数 ...
随机推荐
- ECSTORE2.0 定时任务配置
今天在配置ecstore2.0的定时任务时候,发现ECOS后台每30秒通过JS调用 http://localhost/ecstore-bugfix/index.php/shopadmin/index. ...
- Java Tomcat 中调用.net DLL的方法
近日一个java的项目,客户要求项目中必须使用其提供的加密机制,扔给了两个.net写的DLL.网络上搜了一圈也没找到啥东西,甚至看到人扬言此事绝无可能.郁闷当中考虑了一个思路.用C#做一个Com,调用 ...
- 列表框List Box
List Box/Check List Box ListBox窗口用来列出一系列的文本,每条文本占一行.创建一个列表窗口可以使用成员函数: BOOL CListBox::Create( LPCTSTR ...
- linux常用命令(5)rmdir命令
rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删除某目录时也必须具有对父目录的写权限.1.命令格 ...
- Swift—静态方法-备
静态方法与静态属性类似,Swift中定义了静态方法,也称为类型方法.静态方法的定义与静态属性类似,枚举和结构体的静态方法使用的关键字是static:类静态方法使用的关键字是class或static,如 ...
- 『C # 开发』VS 2008 修改默认生成代码模版
作为Coder,或许会因为每次写代码前要把版权信息Ctrl+C/V上去而蛋疼 ╮(╯▽╰)╭ 可作为Code Copyer,又何尝不蛋疼呢,怎么会容忍作业上署上别人的姓名,学号,XXX 还是要先S ...
- Android 开源框架ActionBarSherlock初探
1:我的开发环境是Ubuntu12.04下:adt-bundle-linux-x86-20130729. ActionBarSherlock下载地址:http://actionbarsherlock. ...
- PHP and java
问题补充: 为什么不选用优秀的ASP.NET,京东做的不是很成功嘛?-----------------------------------------------------------------首 ...
- Entity Framework with MySQL 学习笔记一(验证标签)
直接上代码 [Table("single_table")] public class SingleTable { [Key] public Int32 id { get; set; ...
- 用vlc SDK创建一个播放视频文件和RTSP流视频的Demo
#include <stdio.h> #include <tchar.h> #include <time.h> #include <windows.h> ...