Skip to main content

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

Build Status Git tag Python Version Compatibility

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).

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

  • appdirs for determining user’s cache directory

    pip install idna
    pip install uritools
    pip install appdirs

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 you 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.

Release files for urlextract 0.9

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 0.9
File Size Uploaded
urlextract-0.9.tar.gz 19.0 kB Details

Built distribution (wheel)

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

Total release size: 35.3 kB

Release files / urlextract-0.9.tar.gz

Download URL urlextract-0.9.tar.gz
Size 19.0 kB
Tags Source
SHA-256 checksum
How to use checksums
07b3935a1102bb7b6f2d0fd0623141dd5603956c286cad5ef597bfc156048e91
BLAKE2b-256 checksum
How to use checksums
a8bf03fc771e7551203affbf6e2383d8e195159ba6ad39fb60f962022a648fc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

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

Download URL urlextract-0.9-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a715ccaa3e97c7043912704c69ba1a4128d10c7c53822c98ab197e1116fa8398
BLAKE2b-256 checksum
How to use checksums
51d29727937438c82c00ec091a1e77b000ee79f5b4a85f47dad16a84dfcd3301
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

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

This release

0.9 This release

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