簡體   English   中英

從站點檢索DOM數據

[英]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#JavaJavaScript模擬這種行為(您已經有了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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM