簡體   English   中英

從 Pastebin 原始數據中獲取數據

[英]Get data from a Pastebin raw

我正在嘗試加載表單,以使其計算 pastebin raw 中的行數並將值返回到文本框。 絞盡腦汁,還是想不通。

textBox1.Text = new WebClient().DownloadString("yourlink").

我正在將我的評論擴展到一個答案。
如前所述,您需要一個HttpRequestWebRequest來獲取字符串的內容。 也許new WebClient().DownloadString(url); ,但我更喜歡使用WebRequest因為 .NET Core 也支持它。

您需要做的是,從提取RAW TextArea對象的內容。 我知道,人們可能會因此而討厭我,但我使用來完成這項任務。 或者,您可以使用解析器。

原始數據包含在具有以下屬性的文本區域中:

<textarea id="paste_code" class="paste_code" name="paste_code" onkeydown="return catchTab(this,event)">

所以模式如下所示:

private static string rgxPatternPasteBinRawContent = @"<textarea id=""paste_code"" class=""paste_code"" name=""paste_code"" onkeydown=""return catchTab\(this,event\)"">(.*)<\/textarea>";

由於代碼分布在多行上,我們的 Regex 必須與單行選項一起使用。

Regex rgx = new Regex(rgxPatternPasteBinRawContent, RegexOptions.Singleline);

現在找到包含 RAW 數據的匹配項:

string htmlContent = await GetHtmlContentFromPage("SomePasteBinURL");
//Possibly your new WebClient().DownloadString("SomePasteBinURL");
//await not necesseraly needed here!

Match match = rgx.Match(htmlContent);
string rawContent = "ERROR: No Raw content found!";
if (match.Groups.Count > 0)
{
    rawContent = match.Groups[1].Value;
}

int numberOfLines = rawContent.Split('\n').Length + 1;

你已經完成了。

WebRequest對我來說是這樣的:

private static async Task<string> GetHtmlContentFromPage(string url)
{
    WebRequest request = WebRequest.CreateHttp(url);
    WebResponse response = await request.GetResponseAsync();
    Stream receiveStream = response.GetResponseStream();
    StreamReader readStream = null;


    readStream = new StreamReader(receiveStream);

    string data = readStream.ReadToEnd();

    response.Dispose();
    readStream.Dispose();

    return data;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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