Extract domains from URLs

Hello everybody,

I'm trying to build a workflow to extract the domains from a given list of URLs. I'm using Knime because I'm not a coder and it allows easy integration with MySQL where the URLs are stored.

The Python Script community node seems to be a good bet to do this. I trust Python's url parsing more than something I could do with the String Manipulation node.

My script so far:

import urlparse
from pandas import DataFrame
output_table = DataFrame([
input_table['URLs']
]).T
output_table['domain'] = output_table.apply(urlparse.urlsplit(input_table['URLs'], scheme = 'netloc', allow_fragments = True))

There aren't many tutorials, templates or even forum topics that deal with the Python Script node, but hopefully a python jedi will answer this post and show us all how to do it.

Workflow attached.

Why not use the filehandling nodes? The combination of "String to URI" and "Extract URI Info" nodes does exactly what you need.
 

Thanks Thor, that works a treat. :)

Hi, I have to extract only the short domain from an URL and der URI node just gives me the full domain part and I did not find any similar problem in the forum yet

Examples:
https://www.abc.def.hello.com/asdasdas-123123
http://asewa.asdhiasd.to/?dasdasd

Wanted Solution:
hello.com
asdhiasd.to

I tried the regex splitter but I do not get the right pattern for this. Maybe anyone could help me?

I would use regex to trim everything to the right of the 3rd “/“, then do a regex split by “.” (Or regex to trim everything left of the 2nd “.” From the right) , then clean it up with a simple formula in the Column Expressions node.

I am still doing a lot of trial and error on RegEx test websites for things like directional character counting, but I am sure one of the RegEx masters on here can just shoot from the hip and give you a formula.

1 Like

Hi @kaimbaum and welcome to the Knime Community.

That’s a 7 year old thread :slight_smile:

First of all, there is no such thing as the “short domain” :slight_smile: What you are looking for is what’s commonly referred to as the domain name or simply domain, though this is also not entirely accurate (hence why I said “commonly referred to” - the proper term is the “root domain” or “apex domain”).

www.hello.com is a subdomain of hello.com for example, and asewa.asdhiasd.to is a subdomain of asdhiasd.to.

Looking at what @iCFO , the idea is good, but unfortunately not all domains follow this structure, so it will not work with all domain names. UK urls for example have domains that ends with .co.uk or .gov.uk (e.g: yahoo.co.uk) for example. Canadian urls also have this kind of domains, for example .qc.ca (e.g: etatcivil.gouv.qc.ca)

So, the proposed solution would not work.

Palladian offers a node called URL Domain Extractor that does exactly what you are looking for:

This is an example of the node in action:
image

Palladian is a very useful extension and offers a lot of tools. I would strongly recommend getting it. However, sometimes there are company policies that do not allow “external” extensions to be installed, and if that is the case for you and can’t get the Palladian extension, you can check what @takbb did there without the Palladian extension:

5 Likes

Awesome node share @bruno29a! In general I always find myself defaulting back to manually solving problems like these. The regex would have gotten pretty complex if it needed to handle domain structures differences that you highlighted, which is a massive time waste vs this solution. I am going to take some time and explore more of these “specialty task” nodes.

3 Likes

Thanks a lot, the Palladian extension is great!

1 Like