有没有办法使用 httpwebrequest 通过 ID 来获取元素?
我了解 httpwebrequest 的基础知识,我并不是在网络浏览器上寻找任何答案。我之前的方法是使用网络浏览器完成的,但由于速度不够,我已经转移到 httpwebrequest 以加快该过程。
我有一个元素的 id,我想在 httpwebrequest 中获取并使用它,但不确定从哪里开始。
谢谢
I understand the basics of httpwebrequest and I'm not looking for any answers on web browsers. My previous method was done using webbrowsers but because of the lack of speed I have transferred over to httpwebrequest to speed up the process.
I have an id of an element that I would like to grab and use in an httpwebrequest but not sure where I would start with that.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,需要明确的是:您无法从
HttpWebRequest
中获取元素,您必须从HttpWebResponse
中获取它(因为这是获取 HTML 的地方) 。HttpWebResponse
为您提供对响应流的访问,您可以通过调用GetResponseStream()
获取该响应流。HtmlDocument
(我建议您使用 HTMLAgilityPack 库)。var Nodes = htmlDoc.DocumentNode.SelectNodes("//*[@id='myname']");
我不太记得了,但也可能有一种方法来获取元素ID:
htmlDoc.GetElementById("myname");
然后您可以遍历节点并在那里执行您需要执行的任何操作。
这是另一个示例: http://blogs.msdn.com/b/joshch/archive/2006/12/10/better-html-parsing-and-validation-with-htmlagilitypack.aspx
OK, so to be clear: you can't grab an element from the
HttpWebRequest
, you have to get it from theHttpWebResponse
(since this is where you get the HTML).HttpWebResponse
provides you access to the response stream, which you can obtain by callingGetResponseStream()
.HtmlDocument
(I recommend that you use the HTMLAgilityPack library for that).var nodes = htmlDoc.DocumentNode.SelectNodes("//*[@id='myname']");
I don't recall exactly, but there may also be a method to get an element by ID:
htmlDoc.GetElementById("myname");
Then you can iterate through the nodes and do whatever you need to do there.
Here is another example: http://blogs.msdn.com/b/joshch/archive/2006/12/10/better-html-parsing-and-validation-with-htmlagilitypack.aspx