使用 C# 在 html 文档中抓取 JavaScript 动态生成的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24130650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scraping data dynamically generated by JavaScript in html document using C#
提问by user3213711
How can I scrape data that are dynamically generated by JavaScript in html document using C#?
如何使用 C# 在 html 文档中抓取 JavaScript 动态生成的数据?
Using WebRequest
and HttpWebResponse
in the C# library, I'm able to get the whole html source code as a string, but the difficulty is that the data I want isn't contained in the source code; the data are generated dynamically by JavaScript.
使用WebRequest
和HttpWebResponse
在C#库中,我能够将整个html源代码作为字符串获取,但困难在于源代码中不包含我想要的数据;数据由 JavaScript 动态生成。
On the other hand, if the data I want are already in the source code, then I'm able to get them easily using Regular Expressions.
另一方面,如果我想要的数据已经在源代码中,那么我可以使用正则表达式轻松获取它们。
I have downloaded HtmlAgilityPack
, but I don't know if it would take care of the case where items are generated dynamically by JavaScript...
我已经下载了HtmlAgilityPack
,但我不知道它是否会处理 JavaScript 动态生成项目的情况......
Thank you very much!
非常感谢你!
采纳答案by Pandepic
When you make the WebRequest you're asking the server to give you the page file, this file's content hasn't yet been parsed/executed by a web browser and so the javascript on it hasn't yet done anything.
当您发出 WebRequest 时,您是在要求服务器为您提供页面文件,但该文件的内容尚未被 Web 浏览器解析/执行,因此其上的 javascript 尚未执行任何操作。
You need to use a tool to execute the JavaScript on the page if you want to see what the page looks like after being parsed by a browser. One option you have is using the built in .net web browser control: http://msdn.microsoft.com/en-au/library/aa752040(v=vs.85).aspx
如果你想看到页面被浏览器解析后的样子,你需要使用一个工具来执行页面上的JavaScript。您拥有的一种选择是使用内置的 .net Web 浏览器控件:http: //msdn.microsoft.com/en-au/library/aa752040(v=vs.85).aspx
The web browser control can navigate to and load the page and then you can query it's DOM which will have been altered by the JavaScript on the page.
Web 浏览器控件可以导航到并加载页面,然后您可以查询它的 DOM,该 DOM 将被页面上的 JavaScript 更改。
EDIT (example):
编辑(示例):
Uri uri = new Uri("http://www.somewebsite.com/somepage.htm");
webBrowserControl.AllowNavigation = true;
// optional but I use this because it stops javascript errors breaking your scraper
webBrowserControl.ScriptErrorsSuppressed = true;
// you want to start scraping after the document is finished loading so do it in the function you pass to this handler
webBrowserControl.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserControl_DocumentCompleted);
webBrowserControl.Navigate(uri);
private void webBrowserControl_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection divs = webBrowserControl.Document.GetElementsByTagName("div");
foreach (HtmlElement div in divs)
{
//do something
}
}
回答by vikramsk
You could take a look at a tool like Selenium for scraping pages which has Javascript.
您可以看看像 Selenium 这样的工具来抓取具有 Javascript 的页面。