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

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-0.12.1.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

urlextract-0.12.1-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: urlextract-0.12.1.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1

File hashes

Hashes for urlextract-0.12.1.tar.gz
Algorithm Hash digest
SHA256 afb1ef7dd7eaa4396e35a862cebc2a2a84fd49e098cc58003576eed8aa483b68
MD5 d256da8270497c098905bb521094e5d6
BLAKE2b-256 bcc260e6f856a3d88ed2bfe95ed83bc4df427340149ede65f66194b92235e9d3

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: urlextract-0.12.1-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1

File hashes

Hashes for urlextract-0.12.1-py3-none-any.whl
Algorithm Hash digest
SHA256 477ab3d2cc5729c03692eb32b50b09eb48360d190a9a3b471acf6d187b4fdff8
MD5 57df40d1ddf180dd5942028f61567f77
BLAKE2b-256 335468927324eb81c08e31587258b027d16785a955b72b24189229b6d2ff3d57

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