使用HttpRequest和HttpResponse从Facebook页面提取数据

时间:2017-07-14 22:57:48

标签: vb.net facebook httpwebrequest httpwebresponse

我正在尝试使用VB.NET从Facebook页面中提取一些商业数据。但是,我没有得到我期望的回应。

Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim responseText As String

request = CType(WebRequest.Create(http://www.facebook.com/Microsoft))
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
request.AllowAutoRedirect = True

response = CType(request.GetResponse(), HttpWebResponse)

如果我查看回复的文本,我会得到这个:

<html><head><title>Redirecting...</title><script>__DEV__=0;_script_path = "XVanityURLController";var uri_re=/^(?:(?:[^:\/?#]+):)?(?:\/\/(?:[^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/,target_domain='';window.location.href.replace(uri_re,function(a,b,c,d){var e,f,g;e=f=b+(c?'?'+c:'');if(d){d=d.replace(/^(!|%21)/,'');g=d.charAt(0);if(g=='/'||g=='\\')e=d.replace(/^[\\\/]+/,'/');}if(e!=f)window.location.replace(target_domain+e);});</script><script type="text/javascript">/*<![CDATA[*/(function(){function si_cj(m){setTimeout(function(){new Image().src="https:\/\/error.facebook.com\/common\/scribe_endpoint.php?c=si_clickjacking&t=956"+"&m="+m;},5000);}if(top!=self && !false){try{if(parent!=top){throw 1;}var si_cj_d=["apps.facebook.com","apps.beta.facebook.com"];var href=top.location.href.toLowerCase();for(var i=0;i<si_cj_d.length;i++){if (href.indexOf(si_cj_d[i])>=0){throw 1;}}si_cj("3 ");}catch(e){si_cj("1 \t");window.document.write("\u003Cstyle>body * {display:none !important;}\u003C\/style>\u003Ca href=\"#\" onclick=\"top.location.href=window.location.href\" style=\"display:block !important;padding:10px\">Go to Facebook.com\u003C\/a>");/*kSxhSBR_*/}}}())/*]]>*/</script><script>window.location.replace("https:\/\/m.facebook.com\/AMD");</script><meta http-equiv="refresh" content="0;url=https://m.facebook.com/AMD" /></head><body></body></html>

但是,当我使用WebBrowser时,它实际上将我重定向到Microsoft页面。我不想使用表格来完成此任务。

所以,我不确定如何使用HttpWebRequest绕过此重定向。我是否需要以某种方式登录Facebook以获得我正在寻找的回复?如果是这样,我该怎么做?请帮忙,我已经好好打了几天。

##

1 个答案:

答案 0 :(得分:1)

该页面使用javascript执行重定向。

您的HttpResponse正在将HTML作为字符串返回,但它不会在其中执行JavaScript。

尝试使用无头网络浏览器,例如Selenium。