def get_img(field,targetImgSrc,resImgSrc): con = connect(host='localhost', user='root', password='root', database='boss', port=3306, charset='utf8mb4') cursor = con.cursor() sql = f"select {field} from jobinfo" cursor.execute(sql) data = cursor.fetchall() text = '' for i,item in enumerate(data): text += item[0] cursor.close() con.close() # 分词 cut = jieba.cut(text) string = ' '.join(cut) print(string) # 图片 img = Image.open(targetImgSrc) # 打开遮罩图片 img_arr = np.array(img) # 将图片转化为列表 wc = WordCloud( background_color='white', mask=img_arr, font_path='STHUPO.TTF' ) wc.generate_from_text(string) # 绘制图片 fig = plt.figure(1) plt.imshow(wc) plt.axis('off') # 不显示坐标轴 # 显示生成的词语图片 # plt.show() # 输入词语图片到文件 plt.savefig(resImgSrc, dpi=500)解释代码
时间: 2024-01-18 17:04:59 浏览: 168
这是一个 Python 函数,用于生成词云图片,具体流程如下:
1. 连接 MySQL 数据库,查询 `jobinfo` 表中的指定字段(`field` 参数)。
2. 将查询结果拼接成一个字符串 `text`。
3. 使用 jieba 库对字符串进行中文分词,得到分词后的字符串 `string`。
4. 打开指定的遮罩图片(`targetImgSrc` 参数),将其转化为列表 `img_arr`。
5. 使用 WordCloud 库生成词云对象 `wc`,并将分词后的字符串 `string` 作为输入,生成词云。
6. 使用 Matplotlib 库绘制词云图片并保存到指定路径(`resImgSrc` 参数)。
需要注意的是,该函数依赖于 jieba、Pillow、numpy、wordcloud 和 matplotlib 等库,需要先安装这些库才能正常运行。另外,该函数的实现还涉及到 MySQL 数据库的连接和查询操作,需要根据实际情况进行配置。
相关问题
def login_in(self): account = self.ui.lineEdit_L_account.text() password = self.ui.lineEdit_L_password.text() account_list = [] password_list = [] conn = pymysql.connect( user='root', pa
我理解你的问题是询问关于给定的代码段中的 pymysql 连接的使用方式。这是一个用于 Python 的 MySQL 数据库的库。该代码段中,使用 pymysql.connect() 函数连接数据库,其中需要提供用户名和密码等信息。具体来说,该函数接受以下参数:
- host: 数据库服务器主机名,默认为本地主机。
- port: 数据库服务器端口号,默认为 3306。
- user: 数据库用户名。
- password: 数据库用户密码。
- database: 数据库名称。
- charset: 数据库字符集,默认为 utf8mb4。
- cursorclass: 游标类,默认为 pymysql.cursors.Cursor。
在该代码段中,除了用户名和密码外,其他信息使用了默认值,因此只需要提供用户名和密码即可连接到 MySQL 数据库。
def get_mnnu_text(url): response = requests.get(url) soup = BeautifulSoup(response.content.decode('utf-8'), 'html.parser') content = soup.find_all('p') text = '\n'.join([p.get_text() for p in content]) 返回文本 URL = 'http://www.mnnu.edu.cn/' 文本 = get_mnnu_text(URL) 打印(文本) def save_to_navicat(text): connection = pymysql.connect( host='localhost', user='root', password='root', database='pa', charset='utf8' ) try: 使用connection.cursor()作为光标: SQL = “INSERT INTO () VALUES (%S)” encoded_text = TEXT.ENCODE('UTF8') cursor.execute(sql, (encoded_text,)) connection.commit() 最后: connection.close() save_to_navicat(text),我要修改url为 https://movie.douban.com/subject/30391186/comments?status=P
好的,你可以按照以下方式修改代码:
```python
import requests
from bs4 import BeautifulSoup
import pymysql
def get_movie_comments(url):
response = requests.get(url)
soup = BeautifulSoup(response.content.decode('utf-8'), 'html.parser')
content = soup.find_all('span', class_='short')
comments = '\n'.join([c.get_text() for c in content])
return comments
url = 'https://movie.douban.com/subject/30391186/comments?status=P'
comments = get_movie_comments(url)
def save_to_navicat(text):
connection = pymysql.connect(
host='localhost',
user='root',
password='root',
database='pa',
charset='utf8'
)
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `my_table` (`content`) VALUES (%s)"
encoded_text = text.encode('utf8')
cursor.execute(sql, (encoded_text,))
connection.commit()
finally:
connection.close()
save_to_navicat(comments)
```
这样就可以获取豆瓣电影《误杀》的所有评论并保存到数据库中了。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)