从网站中提取详细信息

编程入门 行业动态 更新时间:2024-10-11 09:24:44
本文介绍了从网站中提取详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我是asp应用程序的新手。实际上我想从网站上抓一些细节。我可以通过MS Excel中的vba来做到这一点。但是,不幸的是我的Internet Explorer浏览器工作不正常。 因此,我决定通过asp web应用程序进行网络抓取。 我想从以下网站的html代码中删除详细信息。

Hi to all, I am new in asp applications. Actually I want to scrape some details from a web site.I can do this by vba in MS Excel. But, unfortunately my Internet Explorer browser is not working properly. Hence, I have decided to do web scraping by asp web application. I want to scrap details from following html code of a website.

<div class="phone-number"> (310) 703-7939 </div>

在这里,我想获得电话号码,即(310)703- 7939 我用以下代码来获取此信息。

Here, i want to get the phone number given i.e. (310) 703-7939 I have used following code to get this.

protected void btnInnerText_Click(object sender, EventArgs e) { var document = new HtmlDocument(); document.OptionReadEncoding = false; var url = new Uri("weedmaps/dispensaries/california/lax/true-healing-center?c=dispensaries"); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; using (var response = (HttpWebResponse)request.GetResponse()) { using (var stream = response.GetResponseStream()) { document.Load(stream, Encoding.GetEncoding("iso-8859-9")); } } var node = document.DocumentNode.SelectSingleNode("//div[@class='phone-number']"); { if (node != null) TextBox1.Text = node.InnerHtml; else TextBox1.Text = "na"; } }

但它在Textbox1中给出了na而不是必需的电话号码。 亲切,帮我找到正确的结果。 注意: - 我使用过Htmlagilitypack。 提前致谢。

but it gives na in Textbox1 instead of required phone number. kindly, help me to get correct result. Note:- I have used Htmlagilitypack. Thanks in advance.

推荐答案

你好, 简单的解决方案,人们并没有真正告诉你。 以下是执行网络请求的代码: Hello, Simple solution that people don't really tell you. Here is the code to do a web request: public static class WebConnection { public static string GetResponce(string url) { string data = ""; try { WebRequest request = WebRequest.Create(url); request.Proxy = null; request.Credentials = CredentialCache.DefaultCredentials; WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); data = reader.ReadToEnd(); reader.Close(); response.Close(); } catch { } return data; } }

它就像一个魅力。 那么你所做的就是把它叫做:

And it works like a charm. Then what you do is call it:

string responce = WebConnection.GetResponce("myuri"); //tip here int i = 0;//This is a breakpoint trick

因此在int i = 0上创建一个断点;运行应用程序。 应用程序将中断,将鼠标悬停在变量响应上并单击放大镜,将任何文本复制到记事本文本文档中。 现在使用字符串操作,将请求的字符串操作为所需的字符串。 祝你好运!

So create a breakpoint on the int i = 0; and run the app. The app will break, hover over the variable responce and click the magnifying glass, copy any text into a notepad text document. Now using string manipulation, manipulate the requested string into the required string. Good luck!

更多推荐

从网站中提取详细信息

本文发布于:2023-11-08 06:16:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详细信息   网站

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!