这个 Python 代码可以进一步缩小吗?
Can this Python code be further shrunk?
下面是 Python 代码,用于获取一个域中的所有子域。它以包含网站页面源的文件作为输入。第二个参数是域名。例如:"https://www.sometime.com"
。
import re
def getSubDomains(fil,domain):
with open(fil) as f:
subDomainLst = []
for line in f:
m = re.findall(r'\bhref="\https://[\w+\.*]+%s/'%domain,line)
if(m):
for ele in m: subDomainLst.append(ele.split('/')[2])
else:
continue
subDomainLst = list(set(subDomainLst))
for ele in subDomainLst: print ele
def main():
fil1,domain1 = raw_input("Enter the file name\n"),raw_input("Enter the domain\n")
getSubDomains(fil1,domain1)
main() if __name__ == '__main__' else Pass
我试过将内部 'if else statement' 缩小到
for ele in m: subDomainLst.append(ele.split('/')[2]) if(m) else continue
但这给出了一个错误。
上面的代码是否可以进一步缩减(目前为 16 行),使其占用最少的行数并变得更加 pythonic?
您可能希望将 if
语句更改为 try..except
try:
for ele in m: subDomainLst.append(ele.split('/')[2])
except TypeError:
print "OMG m is not iterable!"
或类似的东西
你有两个不同的目标:缩小线条和变得更 pythonic。
这是在一行中,但它不是 pythonic:
import re;fil,domain = raw_input("Enter the file name\n"),raw_input("Enter the domain\n");print '\n'.join(set(ele.split('/')[2] for line in open(fil) for ele in (re.findall(r'\bhref="\https://[\w+\.*]+%s/'%domain,line) or ())))
您不需要添加继续。你可以尝试这样做,尽管我不推荐这样做,因为它会使代码不可读。
subDomainLst = [ele.split('/')[2] for line in f for ele in re.findall(r'\bhref="\https://[\w+\.*]+%s/' % domain, line)]
顺便说一句,你应该将代码缩进 4 个空格,并尽量避免一行难以理解的语句:pythonic 意味着也可读
完整代码:
if __name__ == '__main__':
import re
fil, domain = raw_input("Enter the file name\n"), raw_input("Enter the domain\n")
with open(fil) as f:
print '\n'.join([ele.split('/')[2] for line in f for ele in re.findall(r'\bhref="\https://[\w+\.*]+%s/' % domain, line)])
下面是 Python 代码,用于获取一个域中的所有子域。它以包含网站页面源的文件作为输入。第二个参数是域名。例如:"https://www.sometime.com"
。
import re
def getSubDomains(fil,domain):
with open(fil) as f:
subDomainLst = []
for line in f:
m = re.findall(r'\bhref="\https://[\w+\.*]+%s/'%domain,line)
if(m):
for ele in m: subDomainLst.append(ele.split('/')[2])
else:
continue
subDomainLst = list(set(subDomainLst))
for ele in subDomainLst: print ele
def main():
fil1,domain1 = raw_input("Enter the file name\n"),raw_input("Enter the domain\n")
getSubDomains(fil1,domain1)
main() if __name__ == '__main__' else Pass
我试过将内部 'if else statement' 缩小到
for ele in m: subDomainLst.append(ele.split('/')[2]) if(m) else continue
但这给出了一个错误。
上面的代码是否可以进一步缩减(目前为 16 行),使其占用最少的行数并变得更加 pythonic?
您可能希望将 if
语句更改为 try..except
try:
for ele in m: subDomainLst.append(ele.split('/')[2])
except TypeError:
print "OMG m is not iterable!"
或类似的东西
你有两个不同的目标:缩小线条和变得更 pythonic。 这是在一行中,但它不是 pythonic:
import re;fil,domain = raw_input("Enter the file name\n"),raw_input("Enter the domain\n");print '\n'.join(set(ele.split('/')[2] for line in open(fil) for ele in (re.findall(r'\bhref="\https://[\w+\.*]+%s/'%domain,line) or ())))
您不需要添加继续。你可以尝试这样做,尽管我不推荐这样做,因为它会使代码不可读。
subDomainLst = [ele.split('/')[2] for line in f for ele in re.findall(r'\bhref="\https://[\w+\.*]+%s/' % domain, line)]
顺便说一句,你应该将代码缩进 4 个空格,并尽量避免一行难以理解的语句:pythonic 意味着也可读
完整代码:
if __name__ == '__main__':
import re
fil, domain = raw_input("Enter the file name\n"), raw_input("Enter the domain\n")
with open(fil) as f:
print '\n'.join([ele.split('/')[2] for line in f for ele in re.findall(r'\bhref="\https://[\w+\.*]+%s/' % domain, line)])