How to extract url from html page?

This because there are underline tags with angular brackets in the description. I give up without a library :wink:

I used to use Regex Buddy (to create and test regex) for this. They had some stock regex that was quite good for extracting URLs from text. This is really a great tool but sadly only runs on Windows (and on Linux using Wine), as I recall. Using the tool, you create, test and debug complex regex. You can even optimize the regex for performance. Then, you cut-and-paste the regex into your code or application. I highly recommend this tool. I would be running it now, but sadly my XP machine died and I'm running OSX on the desktop and only Android on the go.

Nevertheless, its a good effort. :slight_smile:

---------- Post updated at 03:02 AM ---------- Previous update was at 02:58 AM ----------

There are also many online regex sites for creating and testing regex as well. But that said, regex is really not the best tool to parse HTML, unless the requirement is really really really simple :slight_smile:

Hi kurumi,

I thought this discussion was about extracting URLs from HTML, not parsing HTML.

There is a difference, you know, between a generic HTML parser, and simply extracting a URL.

URLs can easily be extracted with regex.

The added difficulty here in this case is that besides the url's also the descriptions had to be extracted which in themselves can contain tags with angular bracket, which I used as record separators. This became to complicated with the approach I had chosen, where I wanted to allow the tags to be spread out over multiple lines.. My approach would work fine in many situations, though.

Yes, I understand....

I have seen efficient regex that can easily extract entire URLs, even with tags and more complex, generalized URLs. I don't have them in front of me, so I can't back up my claims at the moment.

Hi Neo

I think extraction/parsing has no real big difference. We are still getting information out of something anyway.

Yup, urls indeed can be easily extracted (or is it? well....:slight_smile: ) . That is, if the requirement is only urls, nothing else. But not so for this particular question/thread since OP wanted to get the inner text as well. As demonstrated by Scrutinizer, its possible using gawk+regex, but there are still some corner cases left out. Anyway, i think OP (where ever he is) will find Scrutinizer's gawk code to be good enough for his purpose.

Different approach :wink:

sed 's|</a>|&\n|g' infile | sed -n '/<a /s|.*<a [^>]*href="\([^"]*\)[^>]*>\(.*\)</a>$|\1 \2|p'

Inner text?

You mean the text inside the URL used for display?

yes.

<a href="http://www.blah.com" ....  > Inner text </a>

This is easy with Perl regex. We run this type of PHP code using Perl regex daily.

heh, Its easy when the regex is already done for us. :slight_smile:
Anyway, if you happen to have it, please post it. I very much like to take a look. thanks in advance.

Hahaha... thanks kurumi, for helping me make a point during this thread.

Yes, regex can be complex, and I have found the online, web-based tools are not very good compared to the fat client ones, like Regex Buddy, that run natively on XP and Wine/Linux.

If you download Regex Buddy (not sure if there is a free version) you can easily find a number of canned complex regex that can extract all the info in URLs, since this is a common task.

Then, if you have some test cases, like cutting-and-pasting an HTML page, like the Google page used in this thread into the tool, you can easily debug, step-by-step.

So putting together a complex regex with the proper tool can be done in a matter of minutes.

I agree that regex can be complex, but just because it is complex, we should not say "it cannot be done only with regex", because I assure you it can.

... and I leave it up to you to do it. However, you may need a proper regex tool (not the simple online ones), and if you have a proper regex tool, you will save (you and your employer) a lot of time and money, because the price of a good regex tool (like Regex Buddy, under $40) can easily save a sys admin or programmer hours of work.

As I mentioned, it is cool to see a number of other solutions, but this is an easy regex problem, with a proper regex tool (complex, without a proper tool). Go get one :smiley:

Hi.

For URL extraction, I prefer lynx as noted in Extract URLs from HTML code using sed ... cheers, drl

Haha, I tend to lean towards regex and string manipulation too if the problem statement is simple. Just that by chance, the requirement by OP requires the inner text as well, hence my solution using n HTML parser.
There are lots of sayings that goes "HTML is not regular, so its difficult to parse HTML" such as Coding Horror: Parsing Html The Cthulhu Way for example.
I tend to agree, partly at least, since i know that if my requirement is simple, a regex will do. If not, then use a parser.

Thanks for introducing Regex Buddy. Its good to know that it can help with constructing regex, debugging etc..I will take a look at it someday. :slight_smile:

On my side, if there is anything too complicated (wrt HTML parsing), i would still go for a HTML parser, its free anyway :slight_smile:

I liked this thread a lot, BTW. I thought some of the code presented was excellent.

All I was trying to say is that I have worked on some complex regex for the forums (for example parsing the man pages and adding links in the manpage section of these forums), and I could not have easily done it without Regex Buddy. I am not ashamed for being lazy and to not write complex regex off the top-of-my head. My biggest issue with Regex Buddy (I purchased a copy, it was so good), is that I am currently mostly running OSX and Android systems, so I don't keep it handy or use it like I used to. I wish there was an OSX version. Even better, I wish there was a version for Android that I could run on my new tablet (when I buy it, LOL), but the developer of Regex Buddy is a hard-core Windows person (He told me UNIX/Linux people don't like to use regex tools... funny, maybe he is right and I'm the only lazy one!)

I really liked all the posts in this thread. Thank you. I'm been busy with DB replication, vBulletin plugins, cleaning up old PHP code. This thread was interesting.

local $/="a>";
while(<DATA>){
  s/\n//g;
  print $1,"\n" if /href="([^"]*)"/;
}
__DATA__
<a href="http://awebsite"  id="awebsite" class="first">website</a><a href="http://bwebsite"  id="bwebsite" class="first">websiteb</a>
<a href="http:
//awebsite"  id="awebsite" class="first">website</a><a href="
http://bwebsite"  id="bwebsite" class="first">websiteb</a>