中国银行外汇牌价抓取代码不再起作用了吗?
我的代码是ASP.NET的,于2008年12月13日突然失效,不知为什么。
抓取到的html代码只有<head>部分。请高手指点!
代码如下:
string html = "";
try
{
DataSet1.WaiHuiDataTable whDT = new DataSet1.WaiHuiDataTable();
string url = "http://www.boc.cn/cn/common/whpj.html";
html = new MyResponse(url).Html;
string pattern = "<td width=\"86\" align=\"center\" valign=\"middle\" bgcolor=\"#FFFFFF\" class=\'nav\'>(.*?)</td>";
MatchCollection matches = Regex.Matches(html, pattern, RegexOptions.IgnoreCase);
string strResult;
i=matches.Count;
strResult = matches[18].Result("$1");
whDT.AddWaiHuiRow("美元", decimal.Parse(strResult) / 100, DateTime.Now);
strResult = matches[90].Result("$1");
whDT.AddWaiHuiRow("欧元", decimal.Parse(strResult) / 100, DateTime.Now);
return whDT;
}
catch (Exception ex)
{
return null;
}
问题补充:MyResponse.cs代码:
Public class MyResponse
{
...
public MyResponse(string url)
{
GetHtmlCode(url);
}
private void GetHtmlCode(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
DateTime begin = DateTime.Now;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
result = response.StatusDescription;
lastModified = response.LastModified;
characterSet = response.CharacterSet;
size = response.ContentLength;
Stream stream = response.GetResponseStream();
DateTime end = DateTime.Now;
cost = end - begin;
StreamReader streamReader = new StreamReader(stream, Encoding.Default);
html = streamReader.ReadToEnd();
stream.Close();
streamReader.Close();
}
...
}
------解决方案--------------------我用你的GetHtmlCode代码是可以拿到的~~
你的正则貌似有问题
string pattern = " <td width=\"86\" align=\"center\" valign=\"middle\" bgcolor=\"#FFFFFF\" class=\'nav\'>(.*?) </td>";
---》
string pattern = " <td width=\"86\" align=\"center\" valign=\"middle\" bgcolor=\"#FFFFFF\" class=\'nav\'>(.*?)</td>";
多了一个空格
另外你的Encoding.Default应该改成人家网站的编码吧~~~
------解决方案--------------------不明白
up下
------解决方案--------------------外汇 换网址了
------解决方案--------------------前两天 我的也抓不下来
一看 它换网址了
------解决方案--------------------
------解决方案--------------------up
------解决方案--------------------
C# code
protected void Page_Load(object sender, EventArgs e)
{
string url = "http://www.boc.cn/cn/common/whpj.html";
string encodeType = "gb2312";
string err = "";
Response.Write(WebClinetPost(url, postData2, encodeType, out err));
}
static string WebClinetPost(string url, string postData, string encodeType, out string err)
{
string uriString = url;
byte[] byteArray;
byte[] responseArray;
Encoding encoding = Encoding.GetEncoding(encodeType);
try
{
WebClient myWebClient = new WebClient();
WebHeaderCollection myWebHeaderCollection;
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
myWebHeaderCollection = myWeb