制作词云的工具有很多,比如在线版的:Wordle、Wordart、ToCloud、图悦等等,这些网站使用都很方便,直接粘贴或者上传自己的文本信息,即可生成优美的词云图片,还允许各种个性化选择,实在是装B炫酷,汇报制图,必备利器。但“人生苦短,我用Python”,本文主要介绍一下利用python的wordcloud库制作文献标题词云的方法。
工欲善其事,必先利其器。先解决两个问题:wordcloud怎么安装?文献哪里找?
wordcloud的安装
如果在linux环境下,直接pip install wordcloud即可,但windows环境下往往会失败,大多是缺乏以来的库文件,这时候可以下载其whl文件,自己本地安装。网址在这里:https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud,找到wordcloud文件列表,其格式如下:wordcloud‑1.5.0‑cp36‑cp36m‑win32.whl,以短横线分割,分别代表名称,版本号,cp后面跟的为对应python的版本号,win后面的数字是你系统的位数,选择对应的版本后,点击即可下载。进入文件目录,按住shift键单击鼠标右键,选择“在此处打开命令窗口”,输入pip install wordcloud‑1.5.0‑cp36‑cp36m‑win32.whl(你自己下载的文件名),当然前提是你的pip、python都已经部署好,并能够从命令行启动。
获取文献列表
推荐一个网站:FTracker,它动态抓取生物学领域全球知名杂志的论文资料,并且免费提供文献订阅和pdf全文下载。该网站需要注册,但十分简单只需要提供一个邮箱或手机号,进入后是这个样子的:
界面十分简洁,按下F12键,审查元素看一下第一条文献的格式:
标签结构清晰简单,每条文献都有数字编号1.,2.,3.,......,可以直接使用正则抓取文献题目,不过为了更清晰的体现抓取过程,本文用BeautifulSoup库。
wordcloud安装好了,文献列表也找到了,是不是已经急不可耐的想试试了?
获取登录Cookie
用注册的帐号登入Ftracker,F12进入开发人员工具界面,找到“Network”选项,选择“Doc”按钮,再单击左侧的html文件,如果没有内容F5刷新一下:
我们只需要Headers部分的Cookie和User-Agent构建请求文件头。
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'Cookie': '__guid=234491800.795271807902761200.1562809104198.348; UM_distinctid=16bdeae7a262d-0cb2591876cf59-36465d60-100200-16bdeae7a27134; PHPSESSID=t9s2oc3nge5ejaom653bh24253; CNZZDATA1263867126=1985607064-1562809113-%7C1565396163;monitor_count=6'
}
获取网页内容
import resquests
url = "http://www.ftracker.net/index.php/Home/Userindex/userindex/p/1.html"
html = requests.get(url, headers=headers)
html.encoding('utf-8') #重编码网页,与Headers中的charset=utf-8保持一致
解析网页
from bs4 import BeautifulSoup
soup = BeautifulSoup(html.content, 'lxml')
获取文献题目列表
txt = "" #用来储存文献标题的文本
try:
for tag in soup.find_all('p'):
txt += tag.a.text
txt += ".\n"
except:
pass
来看一下结果
print(txt)
Scientists glimpse oddball microbe that could help explain rise of complex life.
‘Robochemist’ concocts its own compounds.
Giant freshwater fishes are in alarming decline.
Blackbirds, brains and bleached anemones — July’s best science images.
A versatile cold-molecule collider.
The Bauhaus at 100: science by design.
EWS–FLI1 modulated alternative splicing of ARID1A reveals novel oncogenic function through the BAF
complex.
Completion of LINE integration involves an open ‘4-way’ branched DNA intermediate.
Efficient inhibition of RNA self-primed extension by addition of competing 3′-capture DNA-improved.
RNA synthesis by T7 RNA polymerase.
Cyclic oligoadenylate signalling mediates Mycobacterium tuberculosis CRISPR defence.
The mRNA degradation factor Xrn1 regulates transcription elongation in parallel to Ccr4.
Pol II phosphorylation regulates a switch between transcriptional and splicing condensates.
Syriza may have lost the election, but Greece’s research reforms deserve to stay.
Speculative ‘supergravity’ theory wins US$3-million prize
Wind-down of stem-cell institute leaves a void.
画图
from wordcloud import WordCloud
words = WordCloud(width=640, height=480, # 图片大小
background_color = 'white', # 背景色
max_words = 100, # 最大显示单词数
max_font_size = 60, # 频率最大单词字体大小
).generate(txt)
image = words.to_image()
image.show() #展示图片
words.to_file('C:\\Users\\Administrator\\Desktop'test.png’) #保存图片
WordCloud()有许多参数可选,可以指定输出图片的形状,具体可自行搜索。
上面仅以第一页的内容为例,展示爬取网页制作词云的基本流程,可以使用for循环爬取更多的页面,还可以调整WordCloud()的参数,绘出各种形状的“美图”。