从c#操纵网页 [英] manipulating a web page from c#

查看:107
本文介绍了从c#操纵网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我刚刚度过了一个非常令人沮丧的阵亡将士纪念日假期,试图通过控制台应用程序的下拉框(日期,报告类型)找到在网页上选择数据的方法。我已经尝试过HTMLDoc和HtmlAgilityPack,结果微薄。在搜索网页时,我发现了WebBrowser,但发现通过控制台应用程序无法正常工作。我想要做的是每天从网站检索交换结算数据并将其转换为.csv格式并在我的应用程序中处理它。如果有人在那里做了类似的事情并且可以给出建议我会非常感激。谢谢。

Hello. I''ve just spent an extremely frustrating Memorial Day holiday trying to find ways to select data on a web page via drop down boxes (date, report type) from a console app. I''ve tried HTMLDoc and HtmlAgilityPack with meager results. While scouring the web I discovered WebBrowser but found that this won''t work through a console app. What I want to do is retrieve exchange settlement data on a daily basis from a website and convert it to .csv format and process it in my application. If anybody out there''s done something similar and could give advice I''d greatly appreciate it. Thanks.

推荐答案

忘记自动化任何网络浏览器。您应该已经知道,在幕后,浏览器和Web服务器之间的通信符合名为超文本传输​​协议的协议(是的,即http)。您在浏览器中执行的所有操作都会在此类数据交换中结束。

框架中有一个类可用于直接在http级别上模拟浏览器活动: HttpClient [ ^ ]。

简单样本: http://www.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/ [ ^ ]

复杂样本 http://blogs.msdn.com/b/webdev/archive/2012/08/26/asp-net-web-api-and-httpclient-samples.aspx [ ^ ]



嗯,这取决于您尝试获取的Web应用程序,有多复杂这种互动将是。为了更好地了解背后的过程,我建议您使用 Fiddler [ ^ ]。您必须记录您的步骤并检查浏览器发送的请求以及您获得的答案。如果幸运的话,可以找到您正在寻找的内容的ajax调用。如果没有,您将需要您已经尝试过处理结果的Agility Pack - 当且仅当您已经通过应用程序的逻辑时。
Forget automating any web browser. You should know already that behind the scenes there is a communication between the browser and the web server conform a protocol named Hypertext Transfer Protocol (yes, that is http). Everything you do in the browser ends up in such a data exchange.
There is a class in the framework that you can use to simulate the browser activity directly on http level: HttpClient[^].
Simple samples: http://www.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/[^]
Complex samples http://blogs.msdn.com/b/webdev/archive/2012/08/26/asp-net-web-api-and-httpclient-samples.aspx[^]

Well, it depend on the web application you are trying to fetch, how complex this interaction will be. To get better knowing the process behind, I suggest you use Fiddler[^]. You will have to record your steps and examine the requests sent by the browser and the answers you get. If you are lucky, there is an ajax call for the content you are looking for. If not, you will need the Agility Pack you have already tried to process the result - when, and only when you have stepped trough the logic of the application.


这篇关于从c#操纵网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆