html超链接词云代码示例
时间: 2023-10-29 13:57:02 浏览: 56
这里给出一个使用Python和WordCloud库生成HTML超链接词云的示例代码:
```python
from wordcloud import WordCloud
import random
# 生成词云
text = "Python Java HTML CSS JavaScript Ruby PHP"
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
# 根据词频随机生成颜色
def random_color(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
hue = random.randint(0, 255)
saturation = random.randint(0, 255)
lightness = random.randint(0, 255)
return "hsl({}, {}%, {}%)".format(hue, saturation, lightness)
# 生成HTML超链接词云
html = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML超链接词云</title>
<style>
.wordcloud {
width: 800px;
height: 400px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="wordcloud">
{}
</div>
</body>
</html>
'''
links = ['https://www.python.org/', 'https://www.java.com/', 'https://www.w3.org/html/', 'https://www.w3.org/Style/CSS/', 'https://www.javascript.com/', 'https://www.ruby-lang.org/', 'https://www.php.net/']
words = wordcloud.words_
for word in words:
link = links[random.randint(0, len(links)-1)]
words[word] = '<a href="{}" target="_blank" style="color:{}">{}</a>'.format(link, random_color(), word)
html = html.format(wordcloud.to_html().replace("class=\"wordcloud\"", ""))
# 输出HTML代码
print(html)
```
运行上述代码后,会生成一个HTML词云页面,每个词都会变成一个超链接,点击后会跳转到对应的网址。你可以自己替换`text`变量和`links`列表中的内容,生成你自己的词云。