数据采集问题

wxz280973534 2009-03-01 12:44:31
在我的asp.net页面中有一小快是引用其他网页的部分文字,
比如说,我现在的网页要引用百度的搜索结果.那我在我的网页上输入abc则程序自动去百度上搜索abc(这些都是隐藏运行的),然后返回搜索的结果
一下代码上获取网页的html代码.比如我要百度的,那传入的url就是http://www.baidu.com/,他也确实能返回代码,但是我现在想要百度搜索abc后的结果这就麻烦了
(因为百度上好像有提供专门的接口,我不想用这个,应为没有通用性,而且百度的url是可以带参数的,比如你要搜索aaa,那就可以输入http://www.baidu.com/s?wd=aaa这样也可以搜索,但是这样也不具备通用性)我想要的效果是能不能程序自己在后台填写然后搜索,把结果的页面通过下面的方法返回?
说了这么多,我也不知道自己说清楚了没,希望高人指点!

public string GetHttpData(string Url)
{
string sException = null;
string sRslt = null;
WebResponse oWebRps = null;
WebRequest oWebRqst = WebRequest.Create(Url);
oWebRqst.Timeout = 50000;


try
{
oWebRps = oWebRqst.GetResponse();
}
catch (WebException e)
{
sException = e.Message.ToString();
Response.Write(sException);
}
catch (Exception e)
{
sException = e.ToString();
Response.Write(sException);
}

finally
{
if (oWebRps != null)
{
StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("GB2312"));
sRslt = oStreamRd.ReadToEnd();
oStreamRd.Close();
oWebRps.Close();
}
}
return sRslt;

}
...全文
121 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
可以实现的 首先你需要返回页面的所有内容。

WebClient wc = new WebClient();//创建WebClient对象
string msg;//存储页面返回的内容
try
{
byte[] byDats = wc.DownloadData(this.TextBox1.Text);//下载指定的Url资源
msg = UnicodeEncoding.Default.GetString(byDats);//转换编码
}
catch (Exception ex)
{
msg = ex.Message;
}
msg.IndexOf("你要查询的关键字的索引");//只能查询一个,你可以循环实现

循环自己写代码,很简单的。有问题在讨论
  • 打赏
  • 举报
回复
你的意思 应该是实现页面的搜索。就是差查找页面中是否存在 你所搜索的关键字
wxz280973534 2009-03-01
  • 打赏
  • 举报
回复
UP
wxz280973534 2009-03-01
  • 打赏
  • 举报
回复
感谢一楼的回答,可是我现在想采集的页面并没有一个确定的URL

比如有一个网页A,他上面有个搜索栏,我现在想通过我的页面输入内容,然后自动去网页A中所搜,然后返回搜索内容

所有采集的资料是和搜索的内容挂钩的,我想知道如何然程序,去该页面搜索然后返回内容
lingyin55 2009-03-01
  • 打赏
  • 举报
回复
up
liao5930 2009-03-01
  • 打赏
  • 举报
回复
up
readfuture 2009-03-01
  • 打赏
  • 举报
回复
up
  • 打赏
  • 举报
回复
学习ing
cg2003 2009-03-01
  • 打赏
  • 举报
回复
up
  • 打赏
  • 举报
回复
你这样指定了数据的话就不叫 数据采集了叫做查找数据

/// <summary>
/// 采集事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string[] ss = GetTitle();
for (int i = 0; i < ss.Length; i++)
{
Response.Write(ss[i]);
Response.Write("<br>");
}
}

/// <summary>
/// 分析并整理信息
/// </summary>
/// <returns></returns>
public string[] GetInfo()
{
WebClient wc = new WebClient();//创建WebClient对象
string msg;
try
{
byte[] byDats = wc.DownloadData(this.TextBox1.Text);//下载指定的Url资源
msg = UnicodeEncoding.Default.GetString(byDats);//转换编码
}
catch (Exception ex)
{
msg = ex.Message;
}
#region 正则切割
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"·\s*<a[^>]*>(?<title>[^<]*)</a>\s*<font[^>]*>(?<date>[^<]*)</font>");
string result = string.Empty;
foreach (Match m in reg.Matches(msg))
{
result += m.Groups["title"].Value + ",";//新闻标题
result += m.Groups["date"].Value + "|" + "\r\n";//时间
}
#endregion 正则切割结束

string[] strResult = result.Split('|');
ArrayList al = new ArrayList();
for (int i = 0; i < strResult.Length; i++)
{
al.Add(strResult[i]);

}
string[] ds;
ArrayList al2 = new ArrayList();
for (int i = 0; i < al.Count; i++)
{
ds = al[i].ToString().Split(',');
foreach (string s in ds)
{
al2.Add(s);
}
}
return (string[])al2.ToArray(typeof(string));
}

/// <summary>
/// 获取文章标题
/// </summary>
/// <returns></returns>
public string[] GetTitle()
{
ArrayList al = new ArrayList();
string[] Titles = GetInfo();
for (int i = 0; i < Titles.Length; i++)
{
if ((i % 2) == 0)
{
al.Add(Titles[i]);
}
}
return ((string[])al.ToArray(typeof(string)));
}
/// <summary>
/// 获取文章发布日期
/// </summary>
/// <returns></returns>
public string[] GetDate()
{
ArrayList al = new ArrayList();
string[] Dates = GetInfo();
for (int i = 0; i < Dates.Length; i++)
{
if ((i % 2) != 0)
{
al.Add(Dates[i]);
}
}
return ((string[])al.ToArray(typeof(string)));
}

我这个实现的是采集:http://info.laser.hc360.com/list/z_news_yw.shtml 这个页面的新闻标题和时间

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧