C#模拟打开一个网页之后,怎么用C#代码模拟对网页的操作?

wwwzjuteducn 2010-11-06 11:25:37
C#模拟打开一个网页之后,怎么用C#代码模拟对网页的操作?
比如说打开了www.baidu.com之后,怎么在C#winform窗体中的textbox中输入要搜索的内容,然后点击button就自动进行搜索,然后把所显示的第一条记录内容显示在winform中的另一个textbox中。。。
求各位神牛给力!!
下面是模打开网页的代码。。
namespace CS_Open_IExplore
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "IEXPLORE.EXE";
p.StartInfo.Arguments = "www.baidu.com";
p.Start();

}
}
}
...全文
238 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwzjuteducn 2010-11-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zenghd 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
[/Quote]
还是有点不能理解。。。
我主要是想实现以下功能:
用C#winform实现一个C#程序,连接到某一个网址,比如说某高校的成绩查询网址,然后在winform上的textbox中输入用户名和帐号之后按button确定就能连接到网页中的下一个页面,然后就能查询到自己的成绩,然后显示在winform中的textBox中。。
wwwzjuteducn 2010-11-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zenghd 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
……
[/Quote]
先谢谢了 不知道能不能跑通 明天在研究研究
ZengHD 2010-11-06
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public static string GetHtml(string url)
{
HttpWebRequest wq = (HttpWebRequest)WebRequest.Create(url);
wq.Timeout = 1000000;
wq.Credentials = CredentialCache.DefaultCredentials;
wq.Method = "GET";
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)wq.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
string responseText = streamReader.ReadToEnd();
streamReader.Close();
return responseText;

}
catch (WebException ex)
{
MessageBox.Show(ex.Message );
}
finally
{
if (response != null)
{
response.Close();
response = null;
}

if (wq != null)
{
wq.Abort();
wq = null;
}
}
return "";
}

private void button1_Click(object sender, EventArgs e)
{
string strHTML = GetHtml("http://www.baidu.com/s?wd=%B9%B7%C8%D5%B5%C4360");
strHTML = strHTML.Replace("\n", "").Replace("\r", "").Replace("\t", "");

string pattern = "<font size=\"3\">(?<Info>.*?)</table>";
Regex exp = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);

MatchCollection matchList = exp.Matches(strHTML);
if (matchList.Count == 0)
return;

for (int j = 0; j < matchList.Count; j++)
{
MessageBox.Show(matchList[j].Groups["Info"].Value);
}

}

}

}
wwwzjuteducn 2010-11-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zenghd 的回复:]
分三步
1、分析百度,分析出怎么把要查询的值传给百度
2、获取查询页面的HTML
3、正则表达式分析出目标数据
[/Quote]
第一步明白
第二步是获得该网页的html的源码么?
第三是不是在源代码中进行检索??
ZengHD 2010-11-06
  • 打赏
  • 举报
回复
分三步
1、分析百度,分析出怎么把要查询的值传给百度
2、获取查询页面的HTML
3、正则表达式分析出目标数据

wwwzjuteducn 2010-11-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zenghd 的回复:]
建议使用HttpWebResponse
或者WebBrowser
[/Quote]
有没有具体的代码么??我是新手。。很多组件都没用过
ZengHD 2010-11-06
  • 打赏
  • 举报
回复
建议使用HttpWebResponse
或者WebBrowser

111,129

社区成员

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

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

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