如题,我想用C#写一个能抓取特定网站的内容数据,不知道可不可以实现,知道的帮我下,谢谢~~
解决方案 »
- SortedList<string, Object>, 如何根据key值得到value值?
- Label sy,我想把sy写成动态的怎么写?
- 请教:如何防止Socket恶意发包
- 比较字符串得奇怪问题
- wpf 的相关问题
- DPF轉換的問題
- WPF中Convert问题
- WPF用datagrid,双击事件无响应,求大神帮忙
- vs2003.net开发工具在什么地方下载呀
- 那位兄台知道MSFlexGrid1.AddItem (string item,object index);中的 object index是什么?
- 能否在dataGridView中对某个指定的单元格单独设置样式,比如背景颜色之类。
- 请问水晶报表 数据垂直居中怎么实现?
using System.Net;
using System.IO; namespace MakeAGETRequest_charp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
string sURL;
sURL = "http://www.microsoft.com"; WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL); WebProxy myProxy = new WebProxy("myproxy",80);
myProxy.BypassProxyOnLocal = true; wrGETURL.Proxy = WebProxy.GetDefaultProxy(); Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream(); StreamReader objReader = new StreamReader(objStream); string sLine = "";
int i = 0; while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
Console.WriteLine("{0}:{1}",i,sLine);
}
Console.ReadLine();
}
}
}
Partial Class weather
Inherits System.Web.UI.UserControl
Public web As New System.Net.WebClient
Public Sub GetWeather()
'Dim Content1 As ContentPlaceHolder = Master.FindControl("ContentPlaceHolder1") '找到Content1中的一個控件-1
Dim Stream1 As Stream
Try
Stream1 = web.OpenRead("http://www.121.com.cn/main/index.shtml")
Dim Read As New StreamReader(Stream1, System.Text.Encoding.GetEncoding("GB2312"))
Dim SourceHtml As String = Read.ReadToEnd
Dim TempHtml, ImageUrl
TempHtml = CStr(SourceHtml)
TempHtml = Split(TempHtml, "<td height=""22"" class=""section4""><span class=""section8"">")
TempHtml = Split(TempHtml(1), "</span>")
'Html = "以下內容來源自深圳市氣象局(www.121.com.cn)<br>" & TempHtml
Label1.Text = TempHtml(0) ImageUrl = CStr(SourceHtml)
ImageUrl = Split(ImageUrl, "<img src=main/../weatherImage/")
ImageUrl = Split(ImageUrl(1), ".")
Image1.ImageUrl = "~/main/weatherImage/" & ImageUrl(0) & ".bmp"
Image1.Visible = True
HyperLink1.Text = TempHtml(0)
HyperLink1.NavigateUrl = "weather.aspx"
Catch ex As Exception
HyperLink1.Text = "氣象局網站故障中,無法截取資料!"
End Try End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetWeather()
End Sub
End Class
private string SendUrl(string Host)
{
string StrReturn = "";
WebResponse result = null;
try
{
WebRequest req = WebRequest.Create(Host);
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader sr = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
StrReturn += str;
count = sr.Read(read, 0, 256);
}
}
catch (Exception e)
{
StrReturn += e.ToString();
StrReturn += "找不到请求 URI,或者它的格式不正确";
}
finally
{
if (result != null)
{
result.Close();
}
}
return StrReturn.Trim();
}