下面的webRequest.GetResponse处,抛出404页面不存在异常
string url = "http://www.kyoto-np.co.jp/article.php?mid=P20100202000200&genre=N1&area=Z10";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
using (Stream st = response.GetResponseStream())
using (StreamReader readStream = new StreamReader(st, Encoding.GetEncoding("EUC-JP")))
{
sss = readStream.ReadToEnd();
}
上面的URL在浏览器里面可以访问
请帮我看看代码是否有问题,还是网站做了防盗链限制处理流程是这样的,先访问http://www.kyoto-np.co.jp(这里内容能取到),
从这个页面中取到新闻的路径http://www.kyoto-np.co.jp/article.php?mid=P20100202000200&genre=N1&area=Z10(这里出错),取得新闻内容
string url = "http://www.kyoto-np.co.jp/article.php?mid=P20100202000200&genre=N1&area=Z10";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
using (Stream st = response.GetResponseStream())
using (StreamReader readStream = new StreamReader(st, Encoding.GetEncoding("EUC-JP")))
{
sss = readStream.ReadToEnd();
}
上面的URL在浏览器里面可以访问
请帮我看看代码是否有问题,还是网站做了防盗链限制处理流程是这样的,先访问http://www.kyoto-np.co.jp(这里内容能取到),
从这个页面中取到新闻的路径http://www.kyoto-np.co.jp/article.php?mid=P20100202000200&genre=N1&area=Z10(这里出错),取得新闻内容
看到log文件里面已经把页面内容取下来了,但是在最后抛出异常,不知道为什么 <system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.Net" >
<listeners>
<add name="MyTraceFile"/>
</listeners>
</source>
</sources> <sharedListeners>
<add
name="MyTraceFile"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="System.Net.trace.log"
/>
</sharedListeners> <switches>
<add name="System.Net" value="Verbose" />
</switches> </system.diagnostics>
using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
using (Stream st = response.GetResponseStream())
if(response.Status==HttpStatus.OK)//这里作下判断呢?防止读取过期的URL
{
using (StreamReader readStream = new StreamReader(st, Encoding.GetEncoding("EUC-JP")))
{
sss = readStream.ReadToEnd();
}
}
当然不排除人家采取技术措施防止数据采集。
如果你非要抓取,我看你可以试一试 使用 webbrowser 弄
比如IE网页抓包辅助工具:HttpWatch
下载地址
然后用webbrowser访问你需要的页面。然后抓取你的内容
而是直接把下面的数据写到返回流中:(抓包的结果)HTTP/1.0 404 Not Found
Date: Thu, 04 Feb 2010 08:50:52 GMT
Server: Apache/1.3.33 (Unix) PHP/4.3.10
X-Powered-By: PHP/4.3.10
Content-Type: text/html
X-Cache: MISS from proxy.xa.nec-as.nec.com.cn
X-Cache-Lookup: MISS from proxy.xa.nec-as.nec.com.cn:8080
Via: 1.0 proxy.xa.nec-as.nec.com.cn:8080 (squid/2.6.STABLE21)
Proxy-Connection: close<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<meta name="keywords" content=",,,,,,,,">
<meta name="description" content="">
<meta name="author" content="">
<title></title>
<link href="css/style3.css" rel="stylesheet" type="text/css">
<link rel="icon" type="image/png" href="./kyoto-np.png">
<script language="javascript" src="js/spotlight.js"></script>
<script type="text/javascript" src="js/kanren.js"></script>
<script language="javascript" src="js/AC_RunActiveContent.js"></script>
<script type="text/javascript">
/* ID */
var adingo_type = "news_article";
/* ID */
var adingo_ctxtid = "culture_n1";
</script>
<script type="text/javascript" src="js/showlistings_article.js"></script>
<link type="text/css" rel="StyleSheet" href="css/showlistings.css">
</head>
<body onload="kwdboxBlur()">
<!-- CheckM8 GetResonse看到404就抛异常了。而浏览器则会分析后面的数据去下载那几个JS文件,
进而执行kwdboxBlur()函数。JS里面的处理大概看了看,应该是取得浏览器等信息后
用拼成一个类似下面的地址,然后用ajax去访问的。http://web-jp.ad-v.jp/adam/detect?req=fr&cat=pnj.kyoto-np.inside2&&&BROWSER=IE7&LOC=http://www.kyoto-np.co.jp/article.php\qmid=P20100202000200\agenre=N1\aarea=Z10&WIDTH=778&HEIGHT=383&WIDTH_RANGE=WR_B&DATE=20100204&HOUR=17&ORD=29899876702552624上面这个地址不能直接访问,还要设置Http头的Referer为:http://www.kyoto-np.co.jp/article.php?mid=P20100202000200&genre=N1&area=Z10。楼主可以参考http://www.motobit.com/tips/detpg_net-getresponse-404/上面的方法
让GetResponse不抛出异常,得到后面的html,然后要么直接执行这些JS(Microsoft.JScript),要么分析JS,在代码里面进行等价的操作。
采用Microsoft.JScript的方法
1. 用Microsoft.JScript把指定的javascript输出到你的前台页面并执行。
2. 添加onload方法,读取页面指定的javascript抽取的内容,并用ajax的方法来保存到本地磁盘
此方法的缺点在于, 你得到的javascript文件可能要读取指定的页面控件,如果你页面没有这些控件,很可能会执行不了该javascript分析js文件并采用等价操作
1. 分析javascript, 得到它所抽取的页面url
2. 再次发送request请求并得到最终返回信息推荐采用第二种方法
try
{
response = (HttpWebResponse)webRequest.GetResponse(); }
catch (WebException ex) {
response = (HttpWebResponse)ex.Response;
}