簡體   English   中英

從文件中提取IP地址

[英]Extracting IP addresses from a file

我正在嘗試從Python中的asp文件提取IP地址,該文件看起來像這樣:

onInternalNet = (
        isInNet(hostDNS, "147.163.1.0", "255.255.0.0") ||
        isInNet(hostDNS, "123.264.0.0", "255.255.0.0") ||
        isInNet(hostDNS, "137.5.0.0", "255.0.0.0") ||
        isInNet(hostDNS, "100.01.02.0", "255.0.0.0") ||
        isInNet(hostDNS, "172.146.30.0", "255.240.0.0") ||
        isInNet(hostDNS, "112.268.0.0", "255.255.0.0") ||

我試圖提取它們的方法是使用正則表達式:

if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):

但是我遇到一個錯誤:

Traceback (most recent call last):
  File "pull_proxy.py", line 27, in <module>
    write_to_file(extract_proxies(in_file), out_file)
  File "pull_proxy.py", line 8, in extract_proxies
    if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
  File "C:\Python27\lib\re.py", line 194, in compile
    return _compile(pattern, flags)
  File "C:\Python27\lib\re.py", line 233, in _compile
    bypass_cache = flags & DEBUG
TypeError: unsupported operand type(s) for &: 'str' and 'int'

我不明白為什么會收到該錯誤,該如何處理該代碼以使其像我希望的那樣提取信息?

import re

def extract_proxies(in_file):
    buffer = []

    for line in in_file:
        if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
            print "{} appened to buffer.".format(line)
            buffer.append(line)
        else:
            pass

    return buffer

def write_to_file(buffer, out_file):
    for proxy in buffer:
        with open(out_file, "a+") as res:
            res.write(proxy)

if __name__ == '__main__':
    print "Running...."
    in_file = "C:/Users/thomas_j_perkins/Downloads/test.asp"
    out_file = "c:/users/thomas_j_perkins/Downloads/results.txt"
    write_to_file(extract_proxies(in_file), out_file)

編輯

意識到我沒有打開文件:

import re

def extract_proxies(in_file):
    buffer = []

    for line in in_file:
        if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
            print "{} appened to buffer.".format(line)
            buffer.append(line)
        else:
            pass

    in_file.close()
    return buffer

def write_to_file(buffer, out_file):
    for proxy in buffer:
        with open(out_file, "a+") as res:
            res.write(proxy)

if __name__ == '__main__':
    print "Running...."
    in_file = "C:/Users/thomas_j_perkins/Downloads/PAC-Global-Vista.asp"
    out_file = "c:/users/thomas_j_perkins/Downloads/results.txt"
    write_to_file(extract_proxies(open(in_file, "r+")), out_file)

仍然出現相同的錯誤:

Running....
Traceback (most recent call last):
  File "pull_proxy.py", line 28, in <module>
    write_to_file(extract_proxies(open(in_file)), out_file)
  File "pull_proxy.py", line 8, in extract_proxies
    if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
  File "C:\Python27\lib\re.py", line 194, in compile
    return _compile(pattern, flags)
  File "C:\Python27\lib\re.py", line 233, in _compile
    bypass_cache = flags & DEBUG
TypeError: unsupported operand type(s) for &: 'str' and 'int'

re.compile期望沒有line (字符串)的適當的flags參數(整數)。

你應該做re.match而不是re.compile

re.compile

將正則表達式模式編譯為正則表達式對象,可使用其match()search()方法進行匹配...

您的最初錯誤

TypeError: unsupported operand type(s) for &: 'str' and 'int'

正是@Moses在他的回答中所說的。 標志應該是int值,而不是字符串。


您應該編譯一次正則表達式。 同樣,遍歷行時需要使用打開的文件句柄。

匯入

IP_MATCHER = re.compile(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")

def extract_proxies(fh):
    for line in fh:
        line = line.strip()
        match = IP_MATCHER.findall(line)
        if match:
            print "{} appened to buffer.".format(line)
            print match
        else:
            pass



def write_to_file(buffer, out_file):
    for proxy in buffer:
        with open(out_file, "a+") as res:
            res.write(proxy)


if __name__ == '__main__':
    print "Running...."
    in_file = "in.txt"
    with open(in_file) as fh:
        extract_proxies(fh)

如果只想要第一個,它將找到所有匹配項,然后使用IP_MATCHER.searchmatch.groups() 當然,這是假設您實際上要提取IP地址。

例如:

def extract_proxies(fh):
    for line in fh:
        line = line.strip()
        match = IP_MATCHER.findall(line)
        if len(match) == 2:
            print "{} appened to buffer.".format(line)
            ip, mask = match
            print "IP: %s => Mask: %s" % (ip, mask)
        else:
            pass

請檢查以下代碼:

做過幾次改動

  1. re.compile-首先應編譯正則表達式,然后才能與“ match / search / findall”一起使用。
  2. 正則表達式不正確。 在編寫正則表達式時,我們需要從一開始就考慮。 正則表達式沒有直接匹配行之間的單詞。

 import re


    def extract_proxies(in_file):
        buffer1 = []
        #Regex compiled here
        m = re.compile(r'\s*\w+\(\w+,\s+\"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\"')

        for line in in_file:
            #Used here to match
            r = m.match(line)
            if r is not None:
                print "{} appened to buffer.".format(line)
                buffer1.append(r.group(1))
            else:
                pass

        in_file.close()
        return buffer1


    def write_to_file(buffer1, out_file):
        for proxy in buffer1:
            with open(out_file, "a+") as res:
                res.write(proxy+'\n')


    if __name__ == '__main__':
        print "Running...."
        in_file = "sample.txt"
        out_file = "results.txt"
        write_to_file(extract_proxies(open(in_file)), out_file)

輸出:

C:\Users\dinesh_pundkar\Desktop>python c.py
Running....
        isInNet(hostDNS, "147.163.1.0", "255.255.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "123.264.0.0", "255.255.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "137.5.0.0", "255.0.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "100.01.02.0", "255.0.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "172.146.30.0", "255.240.0.0") ||
 appened to buffer.
        isInNet(hostDNS, "112.268.0.0", "255.255.0.0") || appened to buffer.

C:\Users\dinesh_pundkar\Desktop>python c.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM