regex - 正则表达式中的字节错误

标签 regex python-3.x

所以,这是代码:

#!/usr/bin/python
from sys import exit
import urllib.request

answer = urllib.request.urlopen("http://monip.org").read()

def debug(txt):
    print(txt)
    exit(0)

def parse_answer(answer):
    ''' Simple function to parse request's HTML result
        to find the ip in it. Raise RuntimeError if no 
        ip in result and ip else.
    '''
    import re
    pattern = "^\w+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\w+$"
    regexp = re.compile(pattern)
    if regexp.match(regexp, answer):
        m = regexp.search(regexp, answer)
        ip = m.group(0)
        return ip
    else:
        raise RuntimeError

try:
    ip = parse_answer(answer)
except RuntimeError:
    print("Error, check your network configuration.")
    print("Aborting..")
    exit(1)

print("IP:", ip)

这是我写的。此代码旨在为您提供您的公共(public) IP 地址。如果它不能给你任何东西,它会抛出一个运行时错误。

这是错误:

Traceback (most recent call last): File "./ippub", line 27, in ip = parse_answer(answer) File "./ippub", line 19, in parse_answer if regexp.match(regexp, answer): TypeError: 'bytes' object cannot be interpreted as an integer

这意味着“answer”变量是字节,但我想匹配其中的 IP 地址,但由于 python 类型系统,我不能:-)

有什么想法吗?非常感谢!

最佳答案

您有两个不同的问题。

  1. 您需要转换answer到一个字符串,即使 answer有一些有趣的字符不能用 utf-8 很好地解码.

  2. 您错误地调用了正则表达式 API。

这是一个更正的版本,它使用 chr解决问题 1,并使用正确的语法修复问题 2。

#!/usr/bin/python
from sys import exit
import urllib.request
import re


def debug(txt):
    print(txt)
    exit(0)

def parse_answer(answer):
    ''' Simple function to parse request's HTML result
        to find the ip in it. Raise RuntimeError if no 
        ip in result and ip else.
    '''
    answer = "".join([chr(x) for x in answer])
    pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
    regexp = re.compile(pattern)
    m = regexp.search(answer)
    if m:
        ip = m.group(0)
        return ip
    else:
        raise RuntimeError

answer = urllib.request.urlopen("http://monip.org").read()

try:
    ip = parse_answer(answer)
except RuntimeError:
    print("Error, check your network configuration.")
    print("Aborting..")
    exit(1)

print("IP:", ip)

关于regex - 正则表达式中的字节错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073131/

相关文章:

c# 某些单词后值的正则表达式

php - ffmpeg 进度条 - PHP 中的编码百分比

r - 如何删除字符串中的 "' s"?

python - 继承如何与 Python 3 中的 kwargs 一起工作?

python - 如何将新关键字放入 'keyword' 模块?

python-3.x - Python re.finditer 特殊字符错误

javascript - 需要替换变量值格式的正则表达式模式

python - 从日志文件中提取特定的词(不是关键字)

python-3.x - 在亚马逊 sns 中在交易和促销短信之间切换

python - 我如何修复此语句,我的代码不适用于 tensorflow ?