[英]extract text content from web page using asp.net web form
我正在嘗試將頁面加載到asp.net Web表單,並僅從其中提取文本,並在Areatext
顯示提取的文本
像這樣:
我的代碼是:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#form1 {
height: 500px;
width: 1199px;
}
.auto-style1 {}
#TextArea1 {
height: 288px;
width: 1157px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Clike me"
OnClick="Button1_Click" OnClientClick="aspnetForm.target ='_blank';"
Width="160px" CssClass="auto-style1" Height="32px" />
<br />
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>CNN</asp:ListItem>
<asp:ListItem>BBC</asp:ListItem>
<asp:ListItem>FOX</asp:ListItem>
</asp:RadioButtonList>
<br />
<br />
<textarea id="TextArea1" name="S1" runat="server" ></textarea></form>
</body>
</html>
和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
public partial class _Default : System.Web.UI.Page
{
Uri url = null;
WebBrowser wb = new WebBrowser();
protected void Button1_Click(object sender, EventArgs e)
{
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DisplayText);
if (RadioButtonList1.Text == "CNN")
{
url = new Uri("http://www.edition.cnn.com/");
wb.Url = url;
//Response.Redirect(url);
}
else if (RadioButtonList1.Text == "BBC")
{
url = new Uri("http://www.bbc.com/");
wb.Url = url;
}
else
{
url = new Uri("http://www.foxnews.com/");
wb.Url = url;
}
}
private void DisplayText(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
wb.Document.ExecCommand("SelectAll", false, null);
wb.Document.ExecCommand("Copy", false, null);
TextArea1.Value = Clipboard.GetText();
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
但是我有這個錯誤
WebBrowser wb = new WebBrowser();
由於當前線程不在單線程單元中,因此無法實例化ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2'。
所以我做錯了請幫忙,在此先感謝
我從未嘗試過在對象引用中使用WebBrowser,但是我知道這是一個Web表單,這意味着您將收到回發,並且如果每次都重新實例化Browser引用,它將無法運行就像Page對象一樣。 我只是使用Page對象,您可以收集所需的任何控件和方法,同時還可以使用Request / Response名稱空間。 我還將在單選按鈕列表控件上進行匹配,例如以下代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string url;
RadioButtonList rdl = new RadioButtonList();
url = rdl.SelectedItem.Text;
}
}
當然,您只需從基於標記的RadioButtonList中獲取.SelectedItem.Text
,而不是構建一個。
我檢查了一下,似乎WebBrowser
對象也位於System.Windows.Forms
下。 根據我的經驗,您永遠都不想在Web窗體中使用該庫( MsgBox
不良經驗)。
我將使用上面的示例進行重構
Response.Redirect(url);
希望有幫助!
您可能要考慮使用基於其他自動化控件的方法,例如WatiN( 使用Windows Forms WebBrowser來訪問c#asp.net )或HTML Agility Pack(參見網站自動化的最佳方法? )之類的東西。
var root = doc.DocumentNode;
var sb = new StringBuilder();
foreach (var node in root.DescendantNodesAndSelf())
{
if (!node.HasChildNodes)
{
string text = node.InnerText;
if (!string.IsNullOrEmpty(text))
sb.AppendLine(text.Trim());
}
}
顯示如何下載網頁的示例代碼,您可以嘗試以下代碼(從此處獲取 ):
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att);
}
doc.Save("file.htm");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.