Skip to main content

Collects and extracts URLs from given text.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

urlextract-1.9.0.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

urlextract-1.9.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file urlextract-1.9.0.tar.gz.

File metadata

  • Download URL: urlextract-1.9.0.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for urlextract-1.9.0.tar.gz
Algorithm Hash digest
SHA256 70508e02ba9df372e25cf0642db367cece273e8712cd0ce78178fc5dd7ea00db
MD5 4b9a1aed62870797e74bc6c1aa4b83c7
BLAKE2b-256 e11d57a552551f851c6db5e5d66f505548d47dd3f734a8e648f9d4f05e4e5ac9

See more details on using hashes here.

Provenance

File details

Details for the file urlextract-1.9.0-py3-none-any.whl.

File metadata

  • Download URL: urlextract-1.9.0-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for urlextract-1.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f88963532488b1c7c405e21bd162ae97871754ea04b60e18d33ee075b19b82fd
MD5 ce1e73f6e6a04308d2f61380fc08ca0f
BLAKE2b-256 cbcb82ef2994dec66351aaffa45061f30051b6b557bf592952f14657f38b6e43

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page