111,098
社区成员




public partial class test3 : System.Web.UI.Page
{
public static string htmlstr;
protected void Page_Load(object sender, EventArgs e)
{
string url = "";
if (Request.Form["url"] != null)
url = Request.Form["url"].ToString();
url = "http://item.jd.com/1557846746.html";//用于测试
ParameterizedThreadStart ps = new ParameterizedThreadStart(GetHtmlWithBrowser);
Thread t = new Thread(ps);
t.IsBackground = true;
t.ApartmentState = ApartmentState.STA;
t.Start(url);
Response.Write(htmlstr);
}
private static void GetHtmlWithBrowser(object url)
{
htmlstr = string.Empty;
WebBrowser wb = new WebBrowser();
wb.AllowNavigation = true;
wb.Url = new Uri(url.ToString());
DateTime dtime = DateTime.Now;
wb.ScriptErrorsSuppressed = false;
double timespan = 0;
while (timespan < 10 || wb.ReadyState != WebBrowserReadyState.Complete)
{
//Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
DateTime time2 = DateTime.Now;
timespan = (time2 - dtime).TotalSeconds;
}
if (wb.ReadyState == WebBrowserReadyState.Complete)
{
string htmlstr1 = "";
htmlstr1 = wb.Document.Body.OuterHtml;
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlstr1);
//var kwBox = doc.DocumentNode.SelectSingleNode("//div[@class='sku-name']");
var kwBox1 = doc.DocumentNode.SelectSingleNode("//div[@class='store-prompt']");
htmlstr = kwBox1.WriteContentTo();
}
//System.Runtime.InteropServices.Marshal.ReleaseComObject(wb.ActiveXInstance);
wb.Dispose();
}
}