我有两种类型的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"
答案 0 :(得分:2)
我不知道你为什么要用正则表达式来做这件事,但这可行:
if re.search(r't:ac=(\d+)', myURL):
print "numbers"
答案 1 :(得分:1)
您可以使用正则表达式来检查网址是否以数字或字母结尾,例如:
if re.search(r"\d+$", url):
# url ends with numbers
if re.search("[a-z]+$", url, re.IGNORECASE):
# url ends with letters
答案 2 :(得分:1)
您的代码已经或多或少已经正确,但通常在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")