regex 解析URL并在文本文件中提取用户名和密码的Python脚本

jobtbby3  于 2023-05-08  发布在  Python
关注(0)|答案(4)|浏览(203)

我想知道你是否可以帮我这个简单的请求。我确实搜索了一个现有的解决方案,但我不能弄清楚这一点,因为我不是Maven。我需要用户名和密码提取。我会有一个txt文件的网址。我想一个Python脚本读取文件,解析每个URL并提取一个新的文本文件中的username:password。URL可以是.org.net.com等。

http://URL:8080/get.php?username=C59VGdbeJn&password=rNWotM0B6Z&type=list
http://URL:80/get.php?username=D7mPSv9mK6&password=wboR54rDft&type=list
http://URL:80/get.php?username=K8TpaFNvSD&password=3DYIRewM07&type=list
http://URL:8443/get.php?username=KR2gCmAHkq&password=sbhfT2D0B2&type=list
http://URL:80/get.php?username=LA5f3EpWVy&password=oegihMj2mV&type=list
http://URL:80/get.php?username=NHzr2qHgzyDP&password=I9nRDgT5ZK&type=list

文本文件输出将包含:

C59VGdbeJn:rNWotM0B6Z
D7mPSv9mK6:wboR54rDft
K8TpaFNvSD:DYIRewM07
KR2gCmAHkq:sbhfT2D0B2
LA5f3EpWVy:oegihMj2mV
NHzr2qHgzyDP:I9nRDgT5ZK

谢谢各位
祝你今天愉快!
我确实搜索了一个现有的解决方案,但我不能弄清楚这一点,因为我不是Maven。

rjjhvcjd

rjjhvcjd1#

假设你有一个input_file.txt文件中的URL:

http://URL:8080/get.php?username=C59VGdbeJn&password=rNWotM0B6Z&type=list
http://URL:80/get.php?username=D7mPSv9mK6&password=wboR54rDft&type=list
http://URL:80/get.php?username=K8TpaFNvSD&password=3DYIRewM07&type=list
http://URL:8443/get.php?username=KR2gCmAHkq&password=sbhfT2D0B2&type=list
http://URL:80/get.php?username=LA5f3EpWVy&password=oegihMj2mV&type=list
http://URL:80/get.php?username=NHzr2qHgzyDP&password=I9nRDgT5ZK&type=list

你可以试试这个:

import re
output_list = []
with open('input_file.txt') as f:
    for url in f.readlines():
        data = re.search(r'username=(.+)&password=(.+)&', url)
        output_list.append(f"{data.group(1)}:{data.group(2)}\n")

with open('output_file.txt', 'w') as f:
    f.writelines(output_list)

您将获得output_file.txt文件:

C59VGdbeJn:rNWotM0B6Z
D7mPSv9mK6:wboR54rDft
K8TpaFNvSD:3DYIRewM07
KR2gCmAHkq:sbhfT2D0B2
LA5f3EpWVy:oegihMj2mV
NHzr2qHgzyDP:I9nRDgT5ZK

我希望这能解决你的问题。

enyaitl3

enyaitl32#

如果它确实对您有帮助,请将其标记为解决方案:

import re

# Input and output file paths
input_file = "urls.txt"
output_file = "usernames_passwords.txt"

# Regular expression to match username and password
regex = r"username=([^&]*)&password=([^&]*)"

# Open input and output files
with open(input_file, "r") as f_in, open(output_file, "w") as f_out:
    # Loop over each line in the input file
    for line in f_in:
        # Extract the username and password using regex
        match = re.search(regex, line)
        if match:
            username = match.group(1)
            password = match.group(2)
            # Write the username:password to the output file
            f_out.write(f"{username}:{password}\n")
svmlkihl

svmlkihl3#

您可以使用Python内置的**urlparse模块从URL字符串中提取usernamepassword**。
下面是一个示例代码片段,它读取content.txt文件并从每行中提取usernamepassword

from urllib.parse import urlparse, parse_qs

output_file = 'output_file.txt'

with open('content.txt') as f, open(output_file, 'w') as outfile:
    for line in f:
        url = urlparse(line.strip())
        params = parse_qs(url.query)
        username = params['username'][0]
        password = params['password'][0]
        outfile.write(f"{username}:{password}\n")
rdrgkggo

rdrgkggo4#

给定您的输入,假设它存储为位于'/path/to/inputfile'的文件
并且您希望位于'/path/to/outputfile'的文件包含所需的输出。
您可以使用以下命令:

from pathlib import Path

Path('/path/to/outputfile').write_text(
    '\n'.join(f'{a}:{b}'
        for a, b in re.findall(
            r'username=(\w+)&password=(\w+)&',
            Path('/path/to/inputfile').read_text()
        )
    )
)

使用re.findall将一次性找到所有匹配项,从而避免不必要地重复调用re.search

相关问题