5

小白初学 Python ,有没有大佬可以帮忙简化一下代码

 3 years ago
source link: https://www.v2ex.com/t/791165
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

小白初学 Python ,有没有大佬可以帮忙简化一下代码

  LinusChen · 1 天前 · 691 次点击
import pandas as pd
import openpyxl
#用来保存结果的列表
two = []
urltwo = []
urltwoo = []

three = []
urlthree = []
urlthreee = []


four = []
urlfour = []
urlfourr = []

five = []
urlfive = []
urlfivee = []

letters = "abcdefghijklmnopqrstuvwxyz"

#几位数,自己定
ttwo = [a+b for a in letters for b in letters]
tthree = [a+b+c for a in letters for b in letters for c in letters]
ffour = [a+b+c+d for a in letters for b in letters for c in letters for d in letters]
ffive = [a+b+c+d+e for a in letters for b in letters for c in letters for d in letters for e in letters]
#域名后缀
hz = "logistics.com.br"
hz2 = "comex.com"

#开始
for k2 in ttwo:
url2 = [k2 + hz]
url22 = [k2 + hz2]

urltwo.append(url2)
urltwoo.append(url22)
two = urltwo + urltwoo


for k3 in tthree:
url3 = [k3 + hz]
url33 = [k3 + hz2]

urlthree.append(url3)
urlthreee.append(url33)
three = urlthree + urlthreee


for k4 in ffour:
url4 = [k4 + hz]
url44 = [k4 + hz2]

urlfour.append(url4)
urlfourr.append(url44)
four = urlfour + urlfourr

for k5 in ffive:
url5 = [k5 + hz]
url55 = [k5 + hz2]

urlfive.append(url5)
urlfivee.append(url55)
five = urlfive + urlfivee

#导出结果
data = {'两个字母': two,'三个字母': three,'四个字母': four,'五个字母':five}
frame = pd.DataFrame(data)
frame.to_excel("E:\BaiduNetdiskWorkspace\个人\python\猜域名.xlsx")

第 1 条附言  ·  1 天前

#感谢大家的建议,修改了一下代码,但是还是有两个问题,烦请各位大佬指点迷津
#问题 1:不知道怎么处理 letter_permutations 才能组成 ab,ac,ad 这样的组合
#问题 2:怎么才能去掉生成的 excel 第一列默认的数字索引
import string
import itertools
import pandas as pd
import openpyxl
import requests
import sys

#Lowercase_alphabet,小写字母表
Lowercase_alphabet = string.ascii_lowercase

url = []
result_url = []

#定义一个函数来生成 url
def web_guessing(n,domain)
n = input('请输入你想生成几位数的组合')
domain = raw_input('请输入你想组合的域名')
letter_permutations = list(itertools.permutations(Lowercase_alphabet,n))

#不知道怎么处理 letter_permutations 才能组成 ab,ac,ad 这样的字符串组合

#将字母组合和域名组成网址
for permutations in letter_permutations:
url = [permutations+domain]
#检测状态码,储存有效的 url
r=requests.get(url,timeout=5)
code = r.status_code
if code == 200:
result_url.append(url)

#进行数据转换,输出到 excel
data = {'%d位字母的域名组合'%n : result_url}
frame = pd.DataFrame(data)
frame.to_excel("E:\BaiduNetdiskWorkspace\个人\python\域名\猜域名.xlsx")
return("结果保存在:E:\BaiduNetdiskWorkspace\个人\python\域名\猜域名.xlsx")

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK