分享会总结
分享会
一. 简单爬虫实现
1.代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import requests
import re
import os
url = 'https://www.keaitupian.cn/'
response = requests.get(url)
response.encoding = 'utf-8'
#print(response.text)
picter = re.findall(r'<img src="(.*?)"', response.text)
picter.pop(0)
print(picter)
dir_path = 'img'
if not os.path.exists(dir_path):
os.mkdir(dir_path)
for pic in picter:
file_name = pic.split('/')[-1]
file_path = os.path.join(dir_path,file_name)
pic_response = requests.get(pic)
with open(file_path,'wb') as fd:
fd.write(pic_response.content)2.代码解析
爬虫是基于第三方库request来实现的,所以需要手动下载第三方库,request其实就是一个类
下载指令为
1 pip install request下载好后使用import导入,类似于include,这个库可以发送请求获得指定网页的返回数据,然后配合正则表达式来筛选出自己需要的数据
首先提供url(即网址),随后调用request的get方法,得到响应结果response,,并且为了显示中文,更改编码格式为utf-8,这时就已经获得了网页的返回数据
如图
得到源码之后,使用正则表达式来筛选我们所需的数据,由于是抓取图片,所以可以使用开发者工具在网页中查看网页的图片是放在哪些标签当中,是放在
标签当中,所以根据所有的图片的标签格式构建正则表达式<img src=”(.*?)”,然后将筛选到的图片url放在一个数组当中,由于数组的第一个位置是垃圾数据,所以给去掉,结果为
最后使用文件流来将图片给下载到指定文件夹中
1
2
3 dir_path = 'img'
if not os.path.exists(dir_path):
os.mkdir(dir_path)这段代码表示指定路径,如果该路径不存在,则创建路径
1
2
3
4
5
6
7 for pic in picter:
file_name = pic.split('/')[-1]
file_path = os.path.join(dir_path,file_name)
pic_response = requests.get(pic)
with open(file_path,'wb') as fd:
fd.write(pic_response.content)这段代码中
1
2 file_name = pic.split('/')[-1]
file_path = os.path.join(dir_path,file_name)第一句是按“/”拆分字符串,然后取拆分得到的最后一个子字符串当作文件名
第二句的意思是将路径和文件名拼接在一起形成完整的文件路径
1 pic_response = requests.get(pic)这句表示通过图片的url路径下载图片
1
2 with open(file_path,'wb') as fd:
fd.write(pic_response.content)使用文件流写入文件
最后的结果为:
二.使用python创建云图
1.代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 import jieba
import wordcloud
import matplotlib.pyplot as plt
import re
import tkinter as tk
from tkinter import filedialog
import os
def open_file_dialog():
root = tk.Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
if filepath:
return filepath
root.destroy()
file_path = open_file_dialog()
print(file_path)
if file_path:
print('---')
with open(file_path,'r',encoding='utf-8',errors='ignore') as f:
s = f.read()
s = re.sub(r'[^\w\s]','',s)
#分词
lst = jieba.lcut(s)
text = ''
#去掉长度只有一的字,并且去掉换行符
for item in lst:
if '/n' in item:
continue
if len(item) <= 1:
continue
text += item + ' '
print(text)
#创建一个wordcloud对象
wc = wordcloud.WordCloud(width=800, height=600,background_color='white',font_path='C:\\Windows\\Fonts\\simsun.ttc',)
wordcloud = wc.generate(text)
#wc.to_file('F:\\pyproject\\pythonProject\\learnpy\\wordcloud.png')
#绘制词云图
plt.figure(figsize=(10,10))
plt.imshow(wordcloud)
plt.axis('off')
plt.savefig('wordcloud.png')
plt.show()
os.startfile('wordcloud.png')
print('111')2.代码解析
1
2
3
4
5
6
7 import jieba
import wordcloud
import matplotlib.pyplot as plt
import re
import tkinter as tk
from tkinter import filedialog
import os实现云图需要用到jieba,wordcloud matplotlib tkinter 四个第三方库,接下来分别介绍三个库的作用jieba的中文分词工具,wordcloud的是用来生成云图的,matplotlib是用来绘制数据可视化的,Tkinter是Python中的标准GUI库,它提供了一个用于创建图形用户界面的工具包,可以用于创建窗口、按钮、文本框、菜单等各种控件
1
2
3
4
5
6
7
8
9 def open_file_dialog():
root = tk.Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
if filepath:
return filepath
root.destroy()这个函数是用来绘制打开文件窗口的,使用户可以用图形化界面来导入文件
如图
选择文件后便会返回所选文件的路径
1
2
3
4
5
6 if file_path:
print('---')
with open(file_path,'r',encoding='utf-8',errors='ignore') as f:
s = f.read()
s = re.sub(r'[^\w\s]','',s)如果选择了文件,就会进入该分支,使用文件流打开文件,其中open函数的各个参数的含义分别为,文件路径,文件编码,碰到错误字符直接忽略,as关键字可以用来起别名,即将文件描述符命名为f,然后将数据读入s存储,而下面这句正则则是用来只保留中文字符的
1
2
3
4
5
6
7
8
9
10
11
12
13 #分词
lst = jieba.lcut(s)
text = ''
#去掉长度只有一的字,并且去掉换行符
for item in lst:
if '/n' in item:
continue
if len(item) <= 1:
continue
text += item + ' '然后预处理完读入数据后,直接使用jieba的lcut方法进行中文分词,之后去掉长度只有一的字和换行符,然后将所有的中文分词拼接为一个长字符串
如图
1
2
3 #创建一个wordcloud对象
wc = wordcloud.WordCloud(width=800, height=600,background_color='white',font_path='C:\\Windows\\Fonts\\simsun.ttc',)
wordcloud = wc.generate(text)随后开始绘制云图,创建wordcloud对象,其中各个参数的含义为,宽度和高度,背景颜色,已经指定云图显示的文字格式,然后传入要生成云图的字符串,产生云图
1
2
3
4
5
6
7 #绘制词云图
plt.figure(figsize=(10,10))
plt.imshow(wordcloud)
plt.axis('off')
plt.savefig('wordcloud.png')
plt.show()
os.startfile('wordcloud.png')然后开始绘制词云图,
第一句是创建一个新的图形窗口,并设置图形的大小为10*10英寸
第二句是用来显示词云图
第三句是用来关闭坐标轴的,因为词云图不需要坐标轴
第四句用来保存生成的词云图的
第五句是显示显示所有的图像的 和第二句的作用一样
第六句是启动某个文件的,在这里是启动刚才保存的词云图,让他在window显示,而不是只是在pycharm中显示
最后结果如图:
最后可以将脚本文件打包成exe可执行文件
使用命令行窗口下载第三方库
1 pip install pyinstaller然后使用该库打包python脚本
1 pyinstaller --onefile your_script.py这里的
your_script.py
是你的Python脚本文件名。--onefile
参数表示生成单个可执行文件结果如图