如何修复valueError:无效的\ x转义

时间:2019-02-12 09:04:21

标签: python

enter image description here enter image description here def Haveing_ip_address(url):         匹配= re.search(             '((([01]?\ d \ d?| 2 [0-4] \ d | 25 [0-5])\\。([[01]?\ d \ d?| 2 [0-4] \ d | 25 [0-5])\。([[01]?\ d \ d?| 2 [0-4] \ d | 25 [0-5])\。”             '([[01]?\ d \ d?| 2 [0-4] \ d | 25 [0-5])\ /)|' #IPv4             '(((0x [0-9a-fA-F] {1,2})\。(0x [0-9a-fA-F] {1,2})\。(0x [0-9a-fA-F] ] {1,2})\。(0x [0-9a-fA-F] {1,2})\ /)'#十六进制的IPv4             '(?:[a-fA-F0-9] {1,4}:){7} [a-fA-F0-9] {1,4}',网址)#ipv6         如果匹配:             #打印match.group()             返回-1         其他:             #打印'找不到匹配的模式'             返回1

def url_length(url):
    if len(url) < 54:
        return 1
    elif len(url) >= 54 | len(url) <= 75:
        return 0
    else:
        return -1


def shortening_service(url):
    match = re.search('bit\.ly|goo\.gl|shorte\.st|go2l\.ink|x\.co|ow\.ly|t\.co|tinyurl|tr\.im|is\.gd|cli\.gs|'
                      'yfrog\.com|migre\.me|ff\.im|tiny\.cc|url4\.eu|twit\.ac|su\.pr|twurl\.nl|snipurl\.com|'
                      'short\.to|BudURL\.com|ping\.fm|post\.ly|Just\.as|bkite\.com|snipr\.com|fic\.kr|loopt\.us|'
                      'doiop\.com|short\.ie|kl\.am|wp\.me|rubyurl\.com|om\.ly|to\.ly|bit\.do|t\.co|lnkd\.in|'
                      'db\.tt|qr\.ae|adf\.ly|goo\.gl|bitly\.com|cur\.lv|tinyurl\.com|ow\.ly|bit\.ly|ity\.im|'
                      'q\.gs|is\.gd|po\.st|bc\.vc|twitthis\.com|u\.to|j\.mp|buzurl\.com|cutt\.us|u\.bb|yourls\.org|'
                      'x\.co|prettylinkpro\.com|scrnch\.me|filoops\.info|vzturl\.com|qr\.net|1url\.com|tweez\.me|v\.gd|'
                      'tr\.im|link\.zip\.net',
                      url)
    if match:
        return -1
    else:
        return 1


def having_at_symbol(url):
    match = re.search('@', url)
    if match:
        return -1
    else:
        return 1


def double_slash_redirecting(url):
    # since the position starts from, we have given 6 and not 7 which is according to the document
    list = [x.start(0) for x in re.finditer('//', url)]
    if list[len(list) - 1] > 6:
        return -1
    else:
        return 1


def prefix_suffix(domain):
    match = re.search('-', domain)
    if match:
        return -1
    else:
        return 1


def having_sub_domain(url):
    # Here, instead of greater than 1 we will take greater than 3 since the greater than 1 conition is when www and
    # country domain dots are skipped
    # Accordingly other dots will increase by 1
    if having_ip_address(url) == -1:
        match = re.search(
            '(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.'
            '([01]?\\d\\d?|2[0-4]\\d|25[0-5]))|(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}',
            url)
        pos = match.end(0)
        url = url[pos:]
    list = [x.start(0) for x in re.finditer('\.', url)]
    if len(list) <= 3:
        return 1
    elif len(list) == 4:
        return 0
    else:
        return -1

2 个答案:

答案 0 :(得分:0)

在python中,十六进制数字(\ xhh,十六进制值为hh的字符)具有字符串转义符。 如果有,请进行更改。

答案 1 :(得分:0)

enter image description here尝试使用字符串文字r来告诉python这是一个原始字符串(斜线被视为字符而不是转义序列)。

>>> a = '\xAT' # is wrong
File "<stdin>", line
SyntaxError: (unicode error) truncated \xXX escape  

>>> a = r'\xAT' # is fine
>>> print(a)
\xat