HTML入门之随机生成网页链接.py

0 Comments

这是萍乡乐学创客教育培训学校的一节Python编程课的源代码。

"""
  HTML入门之随机生成网页链接.py
"""
import random

def makestr3_6():
    s = 'abcdefghijklmnopqrstuvwxyz0123456789'   # random.choice(s)
    z = ''            # 用于保存要返回的字符串
    for x in range( random.randint(3,6) ):       # 
       z +=  random.choice(s)
    return  z
    # ''.join([random.choice(s) for _ in range(random.randint(3,6)])

f = open("index.html",mode='r')  #read
c = f.read()
f.close()

b = c.split("\n")               # 以换行分隔c,返回列表

for x in range(100):
    a = makestr3_6()
    b.insert(-3,"" + a + "")


c  = "\n".join(b)               # 以换行连接b,返回字符串
 
f = open("index.html",mode='w')  #write
f.write(c)
f.close()

发表评论