HTML数据采集
来源:互联网 发布:1962年印度战争 知乎 编辑:程序博客网 时间:2024/06/16 10:52
============获取html内容
using system.net;
using system.IO;
string url = "http://www.baidu.com";
//实例化WebClinet对象
WebClient myweb = new WebClinet();
//根据指定的url获取流
Stream stream = myweb.OpenRead(url);
//从流中用utf8编码实例化一个读取器
StreamReader sr = new StreamReader(stream,Encoding.utf8);
//从流中读取数据得到字符串
string html = sr.ReadToEnd();
==============使用正则提取特定内容
using system.Text.RegularExpressions;
//引号里面填写正则表达式
Regex r = new Regex("");
//匹配所有项返回一个集合
MatchCollection co = r.Matches(html);
//通过下标获取值
co[0].value
=============获取列表页链接
string url ="http://www.baidu.com/list.html";
string listhtml =gethtml(listurl,Encoding.Default);
//引号里填写正则
Regex rlist = new Regex("");
//匹配所有项返回一个集合
MatchCollection co = rlist.Matches(listhtml);
遍历这个集合
for (int i = 0; i< co.Count; i++)
{
if(co[i].Value.ToString().Contains("article")
{
co[i].Value;
}
}
public static string gethtml(string url,Encoding enc){
WebClient myweb = new WebClinet();
//根据指定的url获取流
Stream stream = myweb.OpenRead(url);
//从流中用utf8编码实例化一个读取器
StreamReader sr = new StreamReaderstream,enc);
//从流中读取数据得到字符串
string html = sr.ReadToEnd();
return(html);
}
====================================================================
文件夹操作 文件操作
string [] drivers = Directory.GetLogicalDrives();//获取本地驱动器列表
Directory.GetCurrentDirectory() //获取当前应用程序的运行目录
string [] files = directory.GetFiles("C:\\") //获取指定目录下面的所有文件列表,返回数组
foreach(string s in files)
{
Console.WriteLine(s);
}
DirectoryInfo din = new DiretoryInfo("c:\\1111");
Console.WriteLine(din.Exists); //判断一个目录是否存在
if(!din.Exists){
din.Create(); //创建文件夹
}
din.Delete(true); //删除目录和它里面的子目录和文件
File.AppendAllText("c://1.txt","\r\n文件不存在自动创建,内容自动追加" );
File.WriteAllLines("c://1.txt",files);
File.WriteAllText("c://1.txt","wwwwwwwwwwwwww");
File.Delete("c://1.txt");
using system.net;
using system.IO;
string url = "http://www.baidu.com";
//实例化WebClinet对象
WebClient myweb = new WebClinet();
//根据指定的url获取流
Stream stream = myweb.OpenRead(url);
//从流中用utf8编码实例化一个读取器
StreamReader sr = new StreamReader(stream,Encoding.utf8);
//从流中读取数据得到字符串
string html = sr.ReadToEnd();
==============使用正则提取特定内容
using system.Text.RegularExpressions;
//引号里面填写正则表达式
Regex r = new Regex("");
//匹配所有项返回一个集合
MatchCollection co = r.Matches(html);
//通过下标获取值
co[0].value
=============获取列表页链接
string url ="http://www.baidu.com/list.html";
string listhtml =gethtml(listurl,Encoding.Default);
//引号里填写正则
Regex rlist = new Regex("");
//匹配所有项返回一个集合
MatchCollection co = rlist.Matches(listhtml);
遍历这个集合
for (int i = 0; i< co.Count; i++)
{
if(co[i].Value.ToString().Contains("article")
{
co[i].Value;
}
}
public static string gethtml(string url,Encoding enc){
WebClient myweb = new WebClinet();
//根据指定的url获取流
Stream stream = myweb.OpenRead(url);
//从流中用utf8编码实例化一个读取器
StreamReader sr = new StreamReaderstream,enc);
//从流中读取数据得到字符串
string html = sr.ReadToEnd();
return(html);
}
====================================================================
文件夹操作 文件操作
string [] drivers = Directory.GetLogicalDrives();//获取本地驱动器列表
Directory.GetCurrentDirectory() //获取当前应用程序的运行目录
string [] files = directory.GetFiles("C:\\") //获取指定目录下面的所有文件列表,返回数组
foreach(string s in files)
{
Console.WriteLine(s);
}
DirectoryInfo din = new DiretoryInfo("c:\\1111");
Console.WriteLine(din.Exists); //判断一个目录是否存在
if(!din.Exists){
din.Create(); //创建文件夹
}
din.Delete(true); //删除目录和它里面的子目录和文件
File.AppendAllText("c://1.txt","\r\n文件不存在自动创建,内容自动追加" );
File.WriteAllLines("c://1.txt",files);
File.WriteAllText("c://1.txt","wwwwwwwwwwwwww");
File.Delete("c://1.txt");
0 0
- HTML数据采集
- HTTP协议、正则表达式、HTML,WEB数据采集基础
- Java进行HTML数据采集:浅谈强大的group正则
- python网络数据采集-Ajax和动态HTML
- .NET 采集数据去掉html标签,以逗号分割的
- 采集数据
- 数据采集
- 数据采集
- html问题采集
- 【网络爬虫】数据采集——将html的数据分析保存到数据库
- ASP网站数据采集程序制作:一个采集入库生成本地文件的几个FUCTION(可用来生成HTML静态网页)
- ASP网站数据采集程序制作:一个采集入库生成本地文件的几个FUCTION(可用来生成HTML静态网页)
- java+Jsoup 正则过滤html网页标签【多线程数据采集之二】
- php实现的一个很好用HTML解析器类可用于采集数据
- java+Jsoup 正则过滤html网页标签【多线程数据采集之二】
- java+Jsoup 正则过滤html网页标签【多线程数据采集之二】
- 网页数据采集系统
- 高速数据采集系统
- Windows7配置QT-Android开发环境!
- zeoslib:程序中删除数据表后,GetTableNames语句仍然显示该表存在的处理
- 2014年工作规划
- 开源软件编译时遇到的问题
- Number Sequence
- HTML数据采集
- 学生管理系统
- 获取屏幕或者控件的宽度高度
- Java 7之集合类型第5篇 - TreeMap
- 简单排序
- Linux的网络设置
- X-UA-Compatible兼容
- Java式实现JS的Escape以及Unescape
- 新年 大iPhone的大期待