我要获取这个网站的数据然后插入到数据库? 网址:http://detail.zol.com.cn/category/272.html需要获取的数据:品牌图片以及连接文字(不要连接地址)。数据库:Sqlserver2000.是一个控制台的程序。编程语言:Csharp(.net)申明:谢谢各位。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //使用下面的函数 读取 你的网页 地址 代码 后对获取的内容 进行分析//--需要引用 using System.Net 以及 using System.IO;private string GetContentFromUrll(string _requestUrl) { string _StrResponse =""; HttpWebRequest _WebRequest = ( HttpWebRequest )WebRequest.Create( _requestUrl ); _WebRequest.Method = "GET"; WebResponse _WebResponse = _WebRequest.GetResponse(); StreamReader _ResponseStream = new StreamReader( _WebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")); _StrResponse = _ResponseStream.ReadToEnd(); _WebResponse.Close(); _ResponseStream.Close(); return _StrResponse; } 一页一页的打开,然后自己输入数据库。为什么我这样说呢,因为这样写出来的程序根本不通用,万一哪天ZOL改变界面了,什么都没用。 给点酬劳,我帮你写.----------------------- CSDN 论坛助手 http://china-csdn.cn hertcloud(·£孙子兵法£·) 的方法 + 正则表达 就可以了 当然是HTML源码里的文件~ 翻页的话 找到翻页字符串规律 就可以了 好啊!这是你告诉我的代码:using System;using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;using System.Net;using System.IO;namespace ConsoleApplication3{ class Program { static void Main(string[] args) { WebClient mywebclient = new WebClient(); mywebclient.Credentials = CredentialCache.DefaultCredentials; byte[] datahtml = mywebclient.DownloadData("http://detail.zol.com.cn/category/272.html"); string yourStr = Encoding.Default.GetString(datahtml); MatchCollection mc = Regex.Matches(yourStr, @"(<div\s+class=""manu_elem"">\s*<div\s+class=""manu_photo"">[\s\S]*?<img\s+src='(?<img>[^']*)'[^>]*>\s*</a>\s*</div>\s*<div\s+class=""manu_name"">(\s*<[^>]*>\s*)+(?<text>[^<>]*)</a>\s*</div>|<div\s+class=""manu_"">[\s\S]*?<a\s+[^>]*>\s*(?<text>[^<>]*)</a>\s*</div>)", RegexOptions.IgnoreCase); foreach (Match m in mc) { Console.WriteLine(m.Groups["text"].Value + "\n" + m.Groups["img"].Value + ""); } Console.ReadLine(); } }}是运行成功了,数据也读取出来了。但是如何插入到数据库里面去啊 我的测试代码,假设数据库为test1,表为mytest,图片地址和文字链接所对应的字段分别为img和text SqlConnection con = new SqlConnection("server=CNN;database=test1;uid=sa;password="); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; con.Open(); string yourStr = richTextBox1.Text; MatchCollection mc = Regex.Matches(yourStr, @"(<div\s+class=""manu_elem"">\s*<div\s+class=""manu_photo"">[\s\S]*?<img\s+src='(?<img>[^']*)'[^>]*>\s*</a>\s*</div>\s*<div\s+class=""manu_name"">(\s*<[^>]*>\s*)+(?<text>[^<>]*)</a>\s*</div>|<div\s+class=""manu_"">[\s\S]*?<a\s+[^>]*>\s*(?<text>[^<>]*)</a>\s*</div>)", RegexOptions.IgnoreCase); foreach (Match m in mc) { //Console.WriteLine(m.Groups["text"].Value + "\n" + m.Groups["img"].Value + ""); cmd.CommandText = "insert into mytest(img, text) values('" + m.Groups["img"].Value.Replace("'","") + "','" + m.Groups["text"].Value.Replace("'","") + "')"; cmd.ExecuteNonQuery(); } con.Close(); LED屏显示 C# listview中的beginEdit()问题 刚才看到C#的短网址算法,哪位能改成Java版本的 如何根据大纲标题在 word2007 中生成目录 如何使电脑重启后,消息队列中的消息还在? 请高手指点一下 【向All提问】接口是什么?如何写一个接口?标题要长长长长长长长长长长长长长长长长长长长长长长长长长长 请问如何关闭多线程打开的窗体? 请教:匹配包含"abc"或"123"的字符串,这个正则表达式怎么写? c# 用户控件问题 电子表格、统计图表控件更新,请多提宝贵意见哦 自定义控件的综合问题
private string GetContentFromUrll(string _requestUrl)
{
string _StrResponse ="";
HttpWebRequest _WebRequest = ( HttpWebRequest )WebRequest.Create( _requestUrl );
_WebRequest.Method = "GET";
WebResponse _WebResponse = _WebRequest.GetResponse();
StreamReader _ResponseStream = new StreamReader( _WebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
_StrResponse = _ResponseStream.ReadToEnd();
_WebResponse.Close();
_ResponseStream.Close();
return _StrResponse;
}
-----------------------
CSDN 论坛助手
http://china-csdn.cn
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
WebClient mywebclient = new WebClient();
mywebclient.Credentials = CredentialCache.DefaultCredentials;
byte[] datahtml = mywebclient.DownloadData("http://detail.zol.com.cn/category/272.html");
string yourStr = Encoding.Default.GetString(datahtml); MatchCollection mc = Regex.Matches(yourStr, @"(<div\s+class=""manu_elem"">\s*<div\s+class=""manu_photo"">[\s\S]*?<img\s+src='(?<img>[^']*)'[^>]*>\s*</a>\s*</div>\s*<div\s+class=""manu_name"">(\s*<[^>]*>\s*)+(?<text>[^<>]*)</a>\s*</div>|<div\s+class=""manu_"">[\s\S]*?<a\s+[^>]*>\s*(?<text>[^<>]*)</a>\s*</div>)", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
Console.WriteLine(m.Groups["text"].Value + "\n" + m.Groups["img"].Value + "");
}
Console.ReadLine();
}
}
}
是运行成功了,数据也读取出来了。但是如何插入到数据库里面去啊
SqlConnection con = new SqlConnection("server=CNN;database=test1;uid=sa;password=");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
string yourStr = richTextBox1.Text;
MatchCollection mc = Regex.Matches(yourStr, @"(<div\s+class=""manu_elem"">\s*<div\s+class=""manu_photo"">[\s\S]*?<img\s+src='(?<img>[^']*)'[^>]*>\s*</a>\s*</div>\s*<div\s+class=""manu_name"">(\s*<[^>]*>\s*)+(?<text>[^<>]*)</a>\s*</div>|<div\s+class=""manu_"">[\s\S]*?<a\s+[^>]*>\s*(?<text>[^<>]*)</a>\s*</div>)", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
//Console.WriteLine(m.Groups["text"].Value + "\n" + m.Groups["img"].Value + "");
cmd.CommandText = "insert into mytest(img, text) values('" + m.Groups["img"].Value.Replace("'","") + "','" + m.Groups["text"].Value.Replace("'","") + "')";
cmd.ExecuteNonQuery();
}
con.Close();