欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

页面爬虫(获取其他页面HTML)加载到自己页面示例

程序员文章站 2024-02-29 17:52:52
复制代码 代码如下: //前台
$(document).ready(function() {...
复制代码 代码如下:

//前台
<div id="showiframe"></div>
$(document).ready(function() {
var url = "@url.action("getpagehtml","catalog")";
$.ajax({
url: url,
type: "post",
datatype:"json",
data: { url: "http://www.baidu.com" },
error: function () {
alert("bbb");
},
success: function (data) {
$("#showiframe").append(data);
//$("#showiframe div").hide();
//$("#showiframe>#container").show();
//$("#showiframe>#container>#content").show();
//$("#showiframe>#container>#content>.cmspage").show();
}
});
});
//后台
//爬虫本质,发送url请求,返回整个页面html
[httppost]
public jsonresult getpagehtml(string url)
{
string pageinfo;
try
{
httpwebrequest myreq = (httpwebrequest)httpwebrequest.create(url);
myreq.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
myreq.useragent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; .net clr 2.0.50727)";
httpwebresponse myrep = (httpwebresponse)myreq.getresponse();
stream mystream = myrep.getresponsestream();
streamreader sr = new streamreader(mystream, encoding.default);
pageinfo = sr.readtoend().tostring();
}
catch
{
pageinfo = "";
}
return json(pageinfo);
}