Search code examples
pythonpython-2.7urlparse

How to check if URL has a numbers or String at the end by Python


I have 2 types of URLS

the first one has numbers at the end of url

www.example.fr/drive/cat.productlist.pagination_0.topage/2?t:ac=3686962/3686315

the second one:

www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText

how can i know my input is like first one or second one?

i tried this:

myURL = http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText
                        parsed_url = urlparse.urlparse(myURL)
                        number2, number3 = urlparse.parse_qs(parsed_url.query)["t:ac"][0].split("/")

                        if ( isinstance( number2, numbers.Number) and isinstance( number3, numbers.Number) ) :
                            print "first"
                        else :
                            print "second"

Solution

  • You can use a regex to check if the url ends with numbers or letters i.e.:

    if re.search(r"\d+$", url):
        # url ends with numbers
    if re.search("[a-z]+$", url, re.IGNORECASE):
        # url ends with letters