473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to capture URLs in HTML file

24
Hello everyone,

I am trying to capture URLs in HTML file which appears like

Expand|Select|Wrap|Line Numbers
  1. <a[string]href[space(s) or nothing]=[space(s) or nothing]["][url]["][string]>
  2.  
I found this code but it does not work well.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Text.RegularExpressions
  2. Public Class Form1
  3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4. Dim rx As New Regex("[<]a[\s][\w\W]*[href=](?<word>\S*)[\s\W\w]*[>]", _
  5. RegexOptions.Compiled Or RegexOptions.IgnoreCase)
  6. Dim text As String = "<a href=http:// name=as>"
  7. Dim matches As MatchCollection = rx.Matches(text)
  8. For Each m As Match In matches
  9. MsgBox(m.Groups("word").Value)
  10. Next
  11. End Sub
  12. End Class
  13.  
Thank You
Feb 1 '09 #1
4 1373
Plater
7,872 Expert 4TB
Assuming the regex matches your given pattern it would probably work.
But remember, not all websites are built with " in the href field (they should be, but aren't)
You might find a single quote ' or no quoting at all
Feb 2 '09 #2
hdbbdh
24
Hello everyone,
Thank you Plater for your answer and your important tip.
I make some modifications in the code and it must be work, but finaly I got only the last URL, then can you help me in this.

Expand|Select|Wrap|Line Numbers
  1. Dim sr As New StreamReader("c:\cas.html")
  2. Dim text As String = sr.ReadToEnd()
  3. sr.Close()
  4. text = text.Replace(Chr(13), "")
  5. text = text.Replace("  ", " ")
  6. Dim spattern As String = "<\s*a\s+[\w\W]*href\s*=[\s'" & Chr(34) & "]*(?<word>[^" & Chr(34) & "'\s]+)[\s\S\W\w]*[>]"
  7. Dim rx As New Regex(spattern, _
  8. RegexOptions.Compiled Or RegexOptions.IgnoreCase)
  9. Dim matches As MatchCollection = rx.Matches(text)
  10. For Each m As Match In matches
  11.     ListBox1.Items.Add(m.Groups("word").Value)
  12. Next
  13.  
The html page that I use is attached, and if you try this code for this page you will get contact.htm

Thank you
Attached Files
File Type: zip cas.zip (11.8 KB, 821 views)
Feb 4 '09 #3
hdbbdh
24
I think I found the solution
Expand|Select|Wrap|Line Numbers
  1. Dim sr As New StreamReader("c:\cas.html")
  2. Dim text As String = sr.ReadToEnd()
  3. sr.Close()
  4. text = text.Replace(Chr(13), "")
  5. Do While InStr(text, "  ")
  6.     text = text.Replace("  ", " ")
  7. Loop
  8. Dim spattern As String = "<\s*a\s+[^>]*href\s*=[\s'" & Chr(34) & "]*(?<word>[^" & Chr(34) & "'\s]+)[^>]*>"
  9. For Each m As Match In Regex.Matches(text, spattern, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
  10.     ListBox1.Items.Add(m.Groups("word").Value)
  11. Next
  12.  
The problem is that when I use \W\w its include > symbol.

Thank you
Feb 4 '09 #4
raids51
59
wouldnt it be easier to use the mshtml class?
Feb 6 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
26
by: Howard Brazee | last post by:
I would like to click on a URL of a html document that will open several URLs at once for me. Does someone have an example of a html document that will do this?
3
by: Philip Herlihy | last post by:
I'm using a number of hyperlinks on an Intranet I'm developing which refer to documents on a network server. The URLs I'm using have the form: file://<server>/<path>/<file>.pdf <server> is an...
2
by: guttyguppy | last post by:
This code: <? //PHP SCRIPT: getimages.php Header("content-type: application/x-javascript"); //This function gets the file names of all images in the current directory //and ouputs them as a...
10
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're...
3
by: =?Utf-8?B?Q2hha3JhdmFydGh5?= | last post by:
Hi All, We are working in a web application and we wanted to capture the full webpage into an image file. FULL WEBPAGE means, the page which is below the visibility of the screen and you need...
6
by: Rob | last post by:
Hello, I'm sure this has come up before. I have need for a collection of all elements/objects in an HTML document that have any kind of an attribute (HTML or CSS) that is making use of a URL to...
4
by: DGS | last post by:
Hi guys, Not a developer, but an admin so please pardon my ignorance. I have an issue that I was hoping to get help with. What I need is for the front page of my site to capture the URL that...
11
by: Steven D'Aprano | last post by:
I'm using urllib.urlretrieve() to download HTML pages, and I've hit a snag with URLs containing ampersands: http://www.example.com/parrot.php?x=1&y=2 Somewhere in the process, urls like the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.