Python 如何生成随机html文档
我想生成完全随机的html源代码,可能来自语法。我想在python中实现这一点,但我不确定如何继续——是否有一个库接受语法并随机遵循其规则,打印路径 想法 我认为随机页面更容易实现,而且比您自己编程的任何页面都要随机。任何设计用于生成随机页面的程序都必须遵守定义html结构的任何规则。由于人类比机器好得多,而且违反了规则,所以来自web的随机页面更可能包含随机发生器无法提供的结构Python 如何生成随机html文档,python,html,random,grammar,Python,Html,Random,Grammar,我想生成完全随机的html源代码,可能来自语法。我想在python中实现这一点,但我不确定如何继续——是否有一个库接受语法并随机遵循其规则,打印路径 想法 我认为随机页面更容易实现,而且比您自己编程的任何页面都要随机。任何设计用于生成随机页面的程序都必须遵守定义html结构的任何规则。由于人类比机器好得多,而且违反了规则,所以来自web的随机页面更可能包含随机发生器无法提供的结构 你不必使用yahoo,可能还有其他的随机链接生成器,或者你可以自己构建。你可以很容易地使用自己的随机html生成器,
你不必使用yahoo,可能还有其他的随机链接生成器,或者你可以自己构建。你可以很容易地使用自己的随机html生成器,它看起来非常像自顶向下的解析器。这里有个基地
def RandomHtml():
yield '<html><body>'
yield '<body>'
yield RandomBody()
yield '</body></html>'
def RandomBody():
yield RandomSection()
if random.randrange(2) == 0:
yield RandomBody()
def RandomSection():
yield '<h1>'
yield RandomSentence()
yield '</h1>'
sentences = random.randrange(5, 20)
for _ in xrange(sentences):
yield RandomSentence()
def RandomSentence():
words = random.randrange(5, 15)
yield (' '.join(RandomWord() for _ in xrange(words)) + '.').capitalize()
def RandomWord():
chars = random.randrange(2, 10)
return ''.join(random.choice(string.ascii_lowercase) for _ in xrange(chars))
def Output(generator):
if isinstance(generator, str):
print generator
else:
for g in generator: Output(g)
Output(RandomHtml())
def RandomHtml():
屈服“
屈服“
屈服体()
屈服“
def RandomBody():
第()节
如果random.randrange(2)==0:
屈服体()
def RandomSection():
屈服“
产生一个句子()
屈服“
句子=随机。随机范围(5,20)
对于x范围内的uu(句子):
产生一个句子()
def randomstation():
单词=随机。随机范围(5,15)
yield(“”.join(RandomWord()表示xrange(words))中的“+”).capitalize()
def RandomWord():
chars=random.randrange(2,10)
返回“”。在xrange(字符)中为uu加入(random.choice(string.ascii_小写))
def输出(发电机):
如果存在(发电机、str):
打印生成器
其他:
发电机中的g:输出(g)
输出(随机HTML())
。。。我想我喜欢这个项目的发展方向;)如果您从未听说过麻省理工学院的paper generator:为HTML提供语法(作为SGML DTD),但我不知道在哪里可以找到SGML乱码生成器库……有一个Python的DTD解析器:。不幸的是,yahoo页面已经不存在了-(
def RandomHtml():
yield '<html><body>'
yield '<body>'
yield RandomBody()
yield '</body></html>'
def RandomBody():
yield RandomSection()
if random.randrange(2) == 0:
yield RandomBody()
def RandomSection():
yield '<h1>'
yield RandomSentence()
yield '</h1>'
sentences = random.randrange(5, 20)
for _ in xrange(sentences):
yield RandomSentence()
def RandomSentence():
words = random.randrange(5, 15)
yield (' '.join(RandomWord() for _ in xrange(words)) + '.').capitalize()
def RandomWord():
chars = random.randrange(2, 10)
return ''.join(random.choice(string.ascii_lowercase) for _ in xrange(chars))
def Output(generator):
if isinstance(generator, str):
print generator
else:
for g in generator: Output(g)
Output(RandomHtml())