如何通过 Python 检查 URL 末尾是否有数字或字符串
How to check if URL has a numbers or String at the end by Python
我有 2 种类型的 URL
第一个在url末尾有数字
www.example.fr/drive/cat.productlist.pagination_0.topage/2?t:ac=3686962/3686315
第二个:
www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText
我怎么知道我的输入是第一个还是第二个?
我试过这个:
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"
您可以使用正则表达式来检查 url 是否以数字或字母结尾,即:
if re.search(r"\d+$", url):
# url ends with numbers
if re.search("[a-z]+$", url, re.IGNORECASE):
# url ends with letters
我不知道你为什么要用正则表达式来做这个,但这行得通:
if re.search(r't:ac=(\d+)', myURL):
print "numbers"
你的代码已经或多或少是正确的,但通常在 python 你只是将数据转换成你想要的数据格式,直到它崩溃(请求原谅而不是许可原则)
所以你可以尝试这样的事情(在 Python3),
from urllib.parse import urlparse, parse_qs
myURL = 'http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText'
query = parse_qs(urlparse(myURL).query)
try:
number2, number3 = query.get('t:ac', [''])[0].split('/')
# do something with the numbers
new_number = int(number2) + int(number3)
print('first')
except ValueError:
# t:ac does not have a slash in it
print('second')
(Python2)
from __future__ import print_function
from urlparse import urlparse, parse_qs
myURL = 'http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText'
query = parse_qs(urlparse(myURL).query)
try:
number2, number3 = query.get('t:ac', [''])[0].split('/')
# do something with the numbers
new_number = int(number2) + int(number3)
print('first')
except ValueError:
# t:ac does not have a slash in it
print('second')
并不是不能请求许可,只是可能看起来不那么优雅
if number2.isdigit() and number3.isdigit():
print("first")
else :
print("second")
我有 2 种类型的 URL
第一个在url末尾有数字
www.example.fr/drive/cat.productlist.pagination_0.topage/2?t:ac=3686962/3686315
第二个:
www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText
我怎么知道我的输入是第一个还是第二个?
我试过这个:
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"
您可以使用正则表达式来检查 url 是否以数字或字母结尾,即:
if re.search(r"\d+$", url):
# url ends with numbers
if re.search("[a-z]+$", url, re.IGNORECASE):
# url ends with letters
我不知道你为什么要用正则表达式来做这个,但这行得通:
if re.search(r't:ac=(\d+)', myURL):
print "numbers"
你的代码已经或多或少是正确的,但通常在 python 你只是将数据转换成你想要的数据格式,直到它崩溃(请求原谅而不是许可原则)
所以你可以尝试这样的事情(在 Python3),
from urllib.parse import urlparse, parse_qs
myURL = 'http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText'
query = parse_qs(urlparse(myURL).query)
try:
number2, number3 = query.get('t:ac', [''])[0].split('/')
# do something with the numbers
new_number = int(number2) + int(number3)
print('first')
except ValueError:
# t:ac does not have a slash in it
print('second')
(Python2)
from __future__ import print_function
from urlparse import urlparse, parse_qs
myURL = 'http://www.example.com/some/cat.productlist.pagination_0.topage/4?t:ac=someText'
query = parse_qs(urlparse(myURL).query)
try:
number2, number3 = query.get('t:ac', [''])[0].split('/')
# do something with the numbers
new_number = int(number2) + int(number3)
print('first')
except ValueError:
# t:ac does not have a slash in it
print('second')
并不是不能请求许可,只是可能看起来不那么优雅
if number2.isdigit() and number3.isdigit():
print("first")
else :
print("second")