要求:从B站编程类排名前1000的视频中,抓取视频标签制作成词云,分析B站的小伙伴们都在学习些什么?
根据上节课爬下来的txt文档,再对每一行数据中的每个URL进行访问,接下来就是用上面爬标签的脚本运行,爬出来的标签保存为新的文件。
下面这个是生成标签文件的脚本:
import requests
import bs4
import re
def get_url():
with open("./综合排序.txt", "r", encoding="utf-8") as file:
file = file.read()
s = re.findall("//(.*?)\+", file)
return s
def get_html(url):
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
res = requests.get(url, headers=headers)
return res.text
def get_tags(url):
text = get_html(url)
soup = bs4.BeautifulSoup(text, "html.parser")
tags = soup.select("ul[class = 'tag-area clearfix'] > li")
# v_tag > ul
tags = [each.a.text for each in tags]
return tags
def main():
i = 0
urls = get_url()
for i in range(1001):
url = urls[i]
url = "http://" + url
text = get_tags(url)
for i in text:
with open('span.txt', 'a', encoding="utf-8") as file:
file.write(str(i) + '\n')
file.close()
if __name__ == '__main__':
main()
词云脚本:
import wordcloud
file = open("./span.txt", encoding="utf-8")
text = file.read()
stopwords = {
'野生技术协会','编程','课程','教育','讲座','编程技术宅','教学','电脑','技术','编程教育','编程入门','开发','科学',
'演示','软件','编程视频教程','编程课程','教学视频','经验分享','IT','编程语言','互联网','考试','考研','科技','语言',
'技术宅','面试','自学','原创','公开课','程序员','学习','课程','教程','计算机','线上课堂','视频教程',
}
wc = wordcloud.WordCloud(font_path="/Users/liujin/project/spider/socker_test/simsun.ttf", stopwords=stopwords)
wc.generate(text)
image = wc.to_image()
image.show()
输出:
image.png
网友评论