Skip to main content

URLExtract

URLExtract is python class for collecting (extracting) URLs from given text based on locating TLD.

Build Git tag Python Version Compatibility PyPI downloads per week PyPI downloads

How does it work

It tries to find any occurrence of TLD in given text. If TLD is found it starts from that position to expand boundaries to both sides searching for “stop character” (usually whitespace, comma, single or double quote).

A dns check option is available to also reject invalid domain names.

NOTE: List of TLDs is downloaded from iana.org to keep you up to date with new TLDs.

Installation

Package is available on PyPI - you can install it via pip.

https://img.shields.io/pypi/v/urlextract.svg https://img.shields.io/pypi/status/urlextract.svg
pip install urlextract

Documentation

Online documentation is published at http://urlextract.readthedocs.io/

Requirements

  • IDNA for converting links to IDNA format

  • uritools for domain name validation

  • platformdirs for determining user’s cache directory

  • dnspython to cache DNS results

    pip install idna
    pip install uritools
    pip install platformdirs
    pip install dnspython

Or you can install the requirements with requirements.txt:

pip install -r requirements.txt

Run tox

Install tox:

pip install tox

Then run it:

tox

Example

You can look at command line program at the end of urlextract.py. But everything you need to know is this:

from urlextract import URLExtract

extractor = URLExtract()
urls = extractor.find_urls("Text with URLs. Let's have URL janlipovsky.cz as an example.")
print(urls) # prints: ['janlipovsky.cz']

Or you can get generator over URLs in text by:

from urlextract import URLExtract

extractor = URLExtract()
example_text = "Text with URLs. Let's have URL janlipovsky.cz as an example."

for url in extractor.gen_urls(example_text):
    print(url) # prints: ['janlipovsky.cz']

Or if you want to just check if there is at least one URL you can do:

from urlextract import URLExtract

extractor = URLExtract()
example_text = "Text with URLs. Let's have URL janlipovsky.cz as an example."

if extractor.has_urls(example_text):
    print("Given text contains some URL")

If you want to have up to date list of TLDs you can use update():

from urlextract import URLExtract

extractor = URLExtract()
extractor.update()

or update_when_older() method:

from urlextract import URLExtract

extractor = URLExtract()
extractor.update_when_older(7) # updates when list is older that 7 days

Known issues

Since TLD can be not only shortcut but also some meaningful word we might see “false matches” when we are searching for URL in some HTML pages. The false match can occur for example in css or JS when you are referring to HTML item using its classes.

Example HTML code:

<p class="bold name">Jan</p>
<style>
  p.bold.name {
    font-weight: bold;
  }
</style>

If this HTML snippet is on the input of urlextract.find_urls() it will return p.bold.name as an URL. Behavior of urlextract is correct, because .name is valid TLD and urlextract just see that there is bold.name valid domain name and p is valid sub-domain.

License

This piece of code is licensed under The MIT License.

Metadata

Release files for urlextract 1.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for urlextract 1.10.0
File Size Uploaded
urlextract-1.10.0.tar.gz 35.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for urlextract 1.10.0
File Interpreter ABI Platform
urlextract-1.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 57.3 kB

Release files / urlextract-1.10.0.tar.gz

Download URL urlextract-1.10.0.tar.gz
Size 35.8 kB
Tags Source
SHA-256 checksum
How to use checksums
55c91403e09f4e1f7d36ba0efb88883e476ef14c8d423bddce098395c90be1d8
BLAKE2b-256 checksum
How to use checksums
ed9d969140497d96df4238930146c47779b2c9eebe1651c6ea073afd3287da13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / urlextract-1.10.0-py3-none-any.whl

Download URL urlextract-1.10.0-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3cbe83ebc12548c76f7f1e37bf632f29e3b0da433337fd4f0535f808dffe6324
BLAKE2b-256 checksum
How to use checksums
c09755f3041c03fd14a3ce7fbc581c6a490b5d4fc08b61df9abad2123833f16f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.10.0 This release

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.14.0

2 release files

0.12.1

2 release files

0.11

2 release files

0.10

2 release files

0.9

2 release files

0.8.3

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4.1

1 release file

0.4

1 release file

0.3.2.6

1 release file

0.3.2.5

1 release file

0.3.2.4

1 release file

0.3.2.3

1 release file

0.3.2.2

1 release file

0.3.2.1

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2.7

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page