爬虫
整体思路
爬取xxx评论,爬取网页后从html页面中把需要的字段信息(顾客id、评论时间、评分、评论内容、口味、环境、服务、店铺ID)提取出来并存储到MYSQL数据库中。
网页爬取和解析
数据存储
使用MYSQL数据库,安装教程参考菜鸟教程,python连接MYSQL数据推荐使用pymysql,同样是推荐菜鸟教程菜鸟教程。我们需要先建立一个数据库和表,然后连接并定义游标,然后写对应的sql语句,最后执行事务,存储部分的代码如下:
#连接MYSQL数据库
db = pymysql.connect("localhost","root","","TESTDB" )
cursor = db.cursor()
#存储爬取到的数据
def save_data(data_dict):
sql = '''INSERT INTO DZDP(cus_id, comment_time, comment_star, cus_comment, kouwei, huanjing, fuwu, shopID) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)'''
value_tup = (data_dict['cus_id']
,data_dict['comment_time']
,data_dict['comment_star']
,data_dict['cus_comment']
,data_dict['kouwei']
,data_dict['huanjing']
,data_dict['fuwu']
,data_dict['shopID']
)
try:
cursor.execute(sql,value_tup)
db.commit()
except:
print('数据库写入失败')
return