[英]Retrieve DOM data from site
当我单击网站上的旧帖子时,是否有机会检索DOM结果:
http://www.facebook.com/FamilyGuy
使用C#或Java? 我听说可以通过onclick
执行脚本并获得结果。 我如何执行此脚本:
onclick="(JSCC.get('j4eb9ad57ab8a19f468880561') && JSCC.get('j4eb9ad57ab8a19f468880561').getHandler())(); return false;"
我认为older posts
链接发送Ajax
请求并将响应附加到页面。 (我不确定。您应该检查页面源代码)。
您可以在C#
, Java
和JavaScript
模拟这种行为(您已经有了javascript的代码)。
编辑:
看来Facebook
使用某种内部API( JSCC
)来加载内容,并且它是未记录的。
我不知道Facebook
Developers的API(您可能想先检查一下),但是如果您想准确地模拟浏览器中发生的情况,那么您可以使用TamperData
来拦截GET
请求,方法是单击more posts
链接并找到请求网址及其参数 。
获取此信息后,您必须Login
到应用程序中的帐户并获取身份验证cookie。
您要求的C#
示例代码:
private CookieContainer GetCookieContainer(string loginURL, string userName, string password)
{
var webRequest = WebRequest.Create(loginURL) as HttpWebRequest;
var responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
// Now you may need to extract some values from the login form and build the POST data with your username and password.
// I don't know what exactly you need to POST but again a TamperData observation will help you to find out.
string postData =String.Format("UserName={0}&Password={1}", userName, password); // I emphasize that this is just an example.
// cookie container
var cookies = new CookieContainer();
// post the login form
webRequest = WebRequest.Create(loginURL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookies;
// write the form values into the request message
var requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
webRequest.GetResponse().Close();
return cookies;
}
然后,您可以使用TamperData
通过分析JSCC.get().getHandler()
请求获得的URL
,对拥有的cookie进行GET
请求,最终得到所需的响应流:
var webRequest = WebRequest.Create(url) as HttpWebRequest;
webRequest.CookieContainer = GetCookieContainer(url, userName, password);
var responseStream = webRequest.GetResponse().GetResponseStream();
您也可以将Selenium
用于浏览器自动化。 它还具有C#
和Java
API(我没有使用Selenium
经验)。
Facebook使用AJAX动态加载其内容。 您可以使用Firebug之类的工具检查发出的请求类型,然后将其复制。
或者,您可以使用浏览器渲染引擎(如webkit)来为您处理JavaScript并显示最终的HTML: http : //webscraping.com/blog/Scraping-JavaScript-webpages-with-webkit/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.