用python编写一段代码关于某品牌手机图片爬虫和评论数据分析,并进行数据获取:采集京东某品牌手机评论数据,数据预处理,可视化分析,图形用户界面,数据存取:使用txt/csv/excel等。网址为https://item.jd.com/100011172363.html,
时间: 2024-04-04 12:34:11 浏览: 72
好的,以下是用 Python 实现该任务的代码示例:
```python
import requests
from bs4 import BeautifulSoup
import json
import pandas as pd
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
# 爬取京东某品牌手机的图片
def crawl_images(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
img_tags = soup.select('.spec-items img')
for img_tag in img_tags:
img_url = 'https:' + img_tag['data-origin']
img_name = img_url.split('/')[-1]
with open(img_name, 'wb') as f:
img = requests.get(img_url, headers=headers).content
f.write(img)
# 爬取京东某品牌手机的评论数据
def crawl_comments(url):
comments_url = url.replace('item', 'comment') + '?pageSize=10&callback=fetchJSON_comment98vv18658'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(comments_url, headers=headers)
json_str = response.text[len('fetchJSON_comment98vv18658('):-2]
comments = json.loads(json_str)['comments']
df = pd.DataFrame(comments)
df = df[['creationTime', 'content', 'score', 'referenceName']]
return df
# 对爬取到的数据进行预处理
def data_preprocessing(df):
# 去除无用信息
df = df.dropna(subset=['content'])
df = df[df['content'].str.strip() != '']
# 去除重复数据
df = df.drop_duplicates()
# 中文分词
df['content'] = df['content'].apply(lambda x: ' '.join(jieba.cut(x)))
return df
# 可视化分析
def data_visualization(df):
# 绘制评分分布图
plt.hist(df['score'], bins=5, alpha=0.5)
plt.xlabel('Score')
plt.ylabel('Count')
plt.title('Score Distribution')
plt.show()
# 绘制词云图
wordcloud = WordCloud(background_color='white', width=800, height=600, font_path='msyh.ttc').generate(' '.join(df['content']))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
# 图形用户界面
def create_gui():
def open_file():
file_path = filedialog.askopenfilename(defaultextension='.csv', filetypes=[('CSV Files', '*.csv'), ('Excel Files', '*.xlsx')])
if file_path:
df = pd.read_csv(file_path)
text.delete('1.0', END)
text.insert('1.0', df.head().to_string())
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension='.csv', filetypes=[('CSV Files', '*.csv'), ('Excel Files', '*.xlsx')])
if file_path:
df.to_csv(file_path, index=False)
def crawl_and_analyze():
url = url_entry.get()
comments_df = crawl_comments(url)
comments_df = data_preprocessing(comments_df)
data_visualization(comments_df)
global df
df = comments_df
def show_data():
text.delete('1.0', END)
text.insert('1.0', df.head().to_string())
root = Tk()
root.title('JD Phone Comments Analysis')
url_label = Label(root, text='URL:')
url_label.grid(row=0, column=0)
url_entry = Entry(root, width=50)
url_entry.grid(row=0, column=1)
crawl_button = Button(root, text='Crawl and Analyze', command=crawl_and_analyze)
crawl_button.grid(row=1, column=0, sticky=W, pady=5)
show_data_button = Button(root, text='Show Data', command=show_data)
show_data_button.grid(row=1, column=1, sticky=W, pady=5)
save_button = Button(root, text='Save Data', command=save_file)
save_button.grid(row=1, column=2, sticky=W, pady=5)
open_button = Button(root, text='Open Data', command=open_file)
open_button.grid(row=1, column=3, sticky=W, pady=5)
text = Text(root, width=80, height=30)
text.grid(row=2, column=0, columnspan=4)
root.mainloop()
if __name__ == '__main__':
url = 'https://item.jd.com/100011172363.html'
crawl_images(url)
create_gui()
```
以上代码中,我们首先定义了 `crawl_images` 和 `crawl_comments` 函数,分别用于爬取京东某品牌手机的图片和评论数据,并返回 `pandas.DataFrame` 类型的数据。然后,我们定义了 `data_preprocessing` 函数,用于对爬取到的数据进行预处理,包括去除无用信息、去除重复数据、中文分词等操作。接着,我们定义了 `data_visualization` 函数,用于绘制评分分布图和评论词云图。最后,我们定义了 `create_gui` 函数,用于创建图形用户界面,方便用户进行数据的查看和操作。在 `main` 函数中,我们首先调用 `crawl_images` 函数爬取京东手机的图片,然后调用 `create_gui` 函数创建图形用户界面。