欢迎查看 Taobao Crawler 的文档!¶
Getting Started¶
安装依赖¶
$ pip install -r requirements.txt
爬取数据¶
from gevent import monkey
monkey.patch_all() # gevent monkey patch all
from taobao_crawler.utils.db import DB
from taobao_crawler.crawler import Crawler
db_config = {'db_user': '', # 数据库用户名
'db_pass': '', # 数据库密码
'db_host': 'localhost', # 数据库地址
'db_port': 27017, # 数据库端口
'db_name': 'taobao'} # 数据库 collection 名称
keywords = ['手机', 'Phone']
mongo = DB(db_config)
crawler = Crawler(keywords, mongo.db)
crawler.run()
Crawler¶
进行商品信息和评论的爬取
Item Crawler¶
进行商品信息和评论的爬取
用法¶
在项目中引入爬虫,示例
生成评论爬虫实例¶
keywords = ['手机', 'Phone']
from taobao_crawler.crawler.item import ItemCrawler
crawler = ItemCrawler(keywords, db)
ItemCrawler(keywords, db)
中的 db
参见 DB
运行商品信息爬虫¶
crawler.run()
数据示例¶
{
"is_crawled" : true,
"seller_id" : "360622108",
"sellerLoc" : "广东 深圳",
"location" : "广东 深圳",
"title" : "4+64G指纹识别!全网通4G智能手机5.5寸大屏",
"item_id" : "561319321061",
"price" : "529.00",
"area" : "深圳",
"sold" : "0"
}
类属性¶
-
class
crawler.item.
ItemCrawler
(keywords, db, timeout=3)¶ Bases:
object
爬取淘宝手机商品记录,插入到 mongodb 数据库中。 插入数据示例:{ “is_crawled” : true, “seller_id” : “360622108”, “sellerLoc” : “广东 深圳”, “location” : “广东 深圳”, “title” : “4+64G指纹识别!全网通4G智能手机5.5寸大屏”, “item_id” : “561319321061”, “price” : “529.00”, “area” : “深圳”, “sold” : “0” }
-
__init__
(keywords, db, timeout=3)¶ 初始化 ItemCrawler 实例
参数: - keywords – 搜索的关键词 list,如 [‘手机’,’Phone’]
- db – 一个 pymongo.MongoClient.db 的实例
- timeout – 爬取超时时间, 默认值为 3
-
run
()¶ 运行商品信息爬虫,插入至数据库中。
-
Rate Crawler¶
对数据库中未爬取评论的商品进行商品评论的爬取
用法¶
在项目中引入爬虫,示例
生成评论爬虫实例¶
from taobao_crawler.crawler.rate import RateCrawler
rate_crawler = RateCrawler(db)
RateCrawler(db)
中的 db
参见 DB
运行评论爬虫¶
rate_crawler.run()
数据示例¶
{
"_id" : ObjectId("5a1d600db0d7ee38b9b0c942"),
"buyCount" : 0,
"useful" : true,
"item_id" : "560697135358",
"rate_id" : NumberLong("331495062062"),
"rateDate" : "2017-11-23 23:16:40",
"rate_content" : "挺棒的手机 快递也快非常满意",
"auctionSku" : "机身颜色:香槟色;套餐类型:官方标配;存储容量:64GB;版本类型:中国大陆",
"anony" : true,
"size_info" : "机身颜色:香槟色;套餐类型:官方标配;存储容量:64GB;版本类型:中国大陆"
}
类属性¶
-
class
crawler.rate.
RateCrawler
(db, timeout=3)¶ Bases:
object
从数据库中寻取未爬取评论的商品,爬取其所有评论,并插入至数据库中。 插入数据示例:{ “buyCount” : 0, “useful” : true, “item_id” : “560697135358”, “rate_id” : NumberLong(“331495062062”), “rateDate” : “2017-11-23 23:16:40”, “rate_content” : “挺棒的手机 快递也快非常满意”, “auctionSku” : “机身颜色:香槟色;套餐类型:官方标配;存储容量:64GB;版本类型:中国大陆”, “anony” : true, “size_info” : “机身颜色:香槟色;套餐类型:官方标配;存储容量:64GB;版本类型:中国大陆” }
-
__init__
(db, timeout=3)¶ 初始化 RateCrawler 实例
参数: - db – 一个 pymongo.MongoClient.db 的实例
- timeout – 爬取超时时间, 默认值为 3
-
run
()¶ 运行商品评论爬虫,插入至数据库中。
-
Size Info Analyzer¶
商品尺寸分析器,由关键词,分类器生成饼状图。
用法¶
生成分析器实例¶
keywords = {'小米': ['米', 'mi'], '苹果': ['苹果', 'apple', 'iphone']}
classifiers = ['16G', '32G', '64G', '128G', '256G']
from taobao_crawler.analyzer.size_info_analyzer import SizeInfoAnalyzer
analyzer = SizeInfoAnalyzer(keywords, classifiers, db)
SizeInfoAnalyzer(keywords, classifiers, db)
中的 db
参见 DB
生成饼状图¶
analyzer.run()
类属性¶
-
class
analyzer.size_info_analyzer.
SizeInfoAnalyzer
(keywords, classifiers, db)¶ Bases:
object
商品尺寸分析器,由关键词,分类器生成饼状图
-
__init__
(keywords, classifiers, db)¶ 参数: - keywords – 一个关键词的字典, 关键词的值为一个包含可能的, 示例: {‘小米’: [‘米’, ‘mi’], ‘苹果’: [‘苹果’, ‘apple’, ‘iphone’]}
- classifiers – 一个分类器的列表, 示例: [‘16G’, ‘32G’, ‘64G’, ‘128G’, ‘256G’]
- db – 一个 pymongo.MongoClient.db 的实例
-
run
()¶ 运行商品尺寸分析器,画出饼状图。
返回: 一个 matplotlib.pyplot 实例
-
Size Info Analyzer with Time¶
商品尺寸分析器,由关键词,分类器生成堆栈图。
用法¶
生成分析器实例¶
keywords = {'小米': ['米', 'mi'], '苹果': ['苹果', 'apple', 'iphone']}
classifiers = ['16G', '32G', '64G', '128G', '256G']
from taobao_crawler.analyzer.size_info_analyzer_with_time import SizeInfoAnalyzerWithTime
analyzer = SizeInfoAnalyzerWithTime(keywords, classifiers, db)
SizeInfoAnalyzerWithTime(keywords, classifiers, db)
中的 db
参见 DB
生成饼状图¶
analyzer.run()
类属性¶
-
class
analyzer.size_info_analyzer_with_time.
SizeInfoAnalyzerWithTime
(keywords, classifiers, db)¶ Bases:
object
商品尺寸分析器,由 keywords, classifier 生成堆栈图
-
__init__
(keywords, classifiers, db)¶ 参数: - keywords – 一个关键词的字典, 关键词的值为一个包含可能的, 示例: {‘小米’: [‘米’, ‘mi’], ‘苹果’: [‘苹果’, ‘apple’, ‘iphone’]}
- classifiers – 一个分类器的列表, 示例: [‘16G’, ‘32G’, ‘64G’, ‘128G’, ‘256G’]
- db – 一个 pymongo.MongoClient.db 的实例
-
run
()¶
-
Sold Number Analyzer¶
商品销量分析器,由关键词,分类器生成堆栈图。
用法¶
生成分析器实例¶
keywords = {'小米': ['米', 'mi'], '苹果': ['苹果', 'apple', 'iphone']}
from taobao_crawler.analyzer.sold_number_analyzer import SoldNumberAnalyzer
analyzer = SoldNumberAnalyzer(keywords, db)
SoldNumberAnalyzer(keywords, db)
中的 db
参见 DB
生成饼状图¶
analyzer.run()
类属性¶
-
class
analyzer.sold_number_analyzer.
SoldNumberAnalyzer
(keywords, db, div=10)¶ Bases:
object
商品销量分析器,由 keywords, 价格生成堆栈图
-
__init__
(keywords, db, div=10)¶ 参数: - keywords – 一个关键词的字典, 关键词的值为一个包含可能的, 示例: {‘小米’: [‘米’, ‘mi’], ‘苹果’: [‘苹果’, ‘apple’, ‘iphone’]}
- db – 一个 pymongo.MongoClient.db 的实例
- div – 划分的价格区间数
-
run
()¶ 运行商品销量分析器,画出堆栈图。
返回: 一个 matplotlib.pyplot 实例
-
Sentiment Analyzer¶
遍历 rates collection, 对当中每个评论进行情感分析,并将其存入 rates_sentiments collection 中。
用法¶
生成分析器实例¶
from taobao_crawler.analyzer.sentiment_analyzer import SentimentAnalyzer
analyzer = SentimentAnalyzer(db)
SentimentAnalyzer(db)
中的 db
参见 DB
运行情感分析器¶
analyzer.run()
类属性¶
-
class
analyzer.sentiment_analyzer.
SentimentAnalyzer
(db)¶ Bases:
object
遍历 rates collection, 对当中每个评论进行情感分析,并将其存入 rates_sentiments collection 中。插入数据示例: {‘item_id’: ‘561319321061’, ‘rate_id’: NumberLong(“331495062062”), ‘score’: 1.00}
-
__init__
(db)¶ 参数: db – 一个 pymongo.MongoClient.db 的实例
-
run
()¶ 运行分析器,分析每个评论,插入数据库中
-
Comment Analyzer¶
根据评论分析词频,保存词云。
用法¶
得到词云
生成评论分析器实例¶
brands = ['vivo', 'oppo', 'mi', 'huawei', 'apple', 'SMARTISAN']
from taobao_crawler.analyzer.comment_analyzer import CommentAnalyzer
comm_analyzer = CommentAnalyzer(db)
comm_analyzer.set_brand(brands)
CommentAnalyzer(db)
中的 db
参见 DB
得到各品牌评论词汇频率比例¶
comm_analyzer.frequency_run()
得到各品牌评论词汇超平均值比例¶
comm_analyzer.times_run()
数据示例¶
[('顶多', 20.856220399929835),
('谁家', 20.856220399929835),
('真没想到', 20.856220399929835),
('炸裂', 20.856220399929835),
('比安卓好', 20.856220399929835),
('有所改善', 20.856220399929835),
('老师', 20.856220399929835),
('直板', 20.856220399929835),
('恭维', 20.856220399929835),
('亲亲', 20.856220399929835)]
类属性¶
-
class
analyzer.comment_analyzer.
CommentAnalyzer
(db, brand_name=('vivo', 'oppo', 'mi', 'huawei', 'apple', 'SMARTISAN'))¶ Bases:
object
利用NLP分析评论,词频得到词云
-
__init__
(db, brand_name=('vivo', 'oppo', 'mi', 'huawei', 'apple', 'SMARTISAN'))¶ 参数: - db – 一个 pymongo.MongoClient.db 的实例
- brand_name – 所研究的所有品牌名
-
brand_to_id
(brand)¶ 通过品牌名得到其下商品
参数: brand – list, 所研究的所有品牌名 返回: iteration, 遍历该品牌下所有商品(dictionary)的iteration
-
draw_freq_wordcloud
(string, color_mask_address, write_file_address)¶ 保存词云
参数: - string – string, 用以生成词云的字符串
- color_mask_address – 背景图片地址
- write_file_address – 图片保存地址
-
draw_more_wordcloud
(most, color_mask_address, write_file_address)¶ 绘制超越平均词频倍数的词云
参数: - most – dictionary {词汇,该词汇频率与平均频率比例}
- color_mask_address – 背景图片地址
- write_file_address – 图片保存地址
-
get_counter
(string)¶ 生成计数器(Counter),表示该字符串中各词汇的频次
参数: string – dictionary {词汇:该词汇频率与平均频率比例} 返回: Counter{词汇:该词汇频率与平均频率比例}
-
get_most
(times, k)¶ 得到倍数词典中最高倍数的前k个词汇
参数: - times – dictionary {词汇,该词汇频率与平均频率比例}
- k – 选取词汇频率最高倍数中前k个
返回: list, [(词汇,比例)]
-
get_ratio
(c)¶ 生成频率(Counter),得到该词汇的出现频率
参数: c – Counter 计数器(Counter),表示该字符串中各词汇的频次 返回: dictionary{词汇:该词汇频率与平均频率比例}
-
get_times
(r0, r, hold)¶ 生成频率(Counter),得到该词汇的出现频率
参数: - r0 – dictionary 表示该字符串中各词汇的频率
- r – dictionary 表示所有评论中各词汇的频次
- hold – float 阈值, 当比例超过该值时输出
返回: dictionary{词汇:该词汇频率与平均频率比例}
-
id_to_comment
(item_id)¶ 通过商品ID得到其评论情感评分
参数: item_id – string, 商品ID 返回: string, 该ID商品所有评论
-
run
()¶ 运行函数 得到各品牌评论词云
-
set_brand
(brand_name)¶ 更改品牌名称
参数: brand_name – list, 品牌名称
-
Final Score Analyzer¶
得到评论的标准评分。
用法¶
根据评论分数分布得到商品最终标准评分
生成评论评分器实例¶
brands = ['vivo', 'oppo', 'mi', 'huawei', 'apple', 'SMARTISAN']
from taobao_crawler.analyzer.final_score_analyzer import FinalScoreAnalyzer
score_analyzer = FinalScoreAnalyzer(db)
score_analyzer.set_brand(brands)
FinalScoreAnalyzer(db)
中的 db
参见 DB
查询某一商品分数分布¶
item_id = ''
score_analyzer.score_distribution_run(item_id)
查询某一商品标准分数¶
item_id = ''
score_analyzer.score_distribution_run(item_id)
保存三种评分两两散点图¶
score_analyzer.compared_score_run()
保存各品牌最终评分图¶
final_scores = self.three_score_to_final_score(self.three_scores, [0.43, 0.25, 0.32])
self.draw_final_score(final_scores)
类属性¶
-
class
analyzer.final_score_analyzer.
FinalScoreAnalyzer
(db, brand_name=('vivo', 'oppo', 'mi', 'huawei', 'apple', 'SMARTISAN'))¶ Bases:
object
利用商品的评论信息,通过三种中间评分,得到一种对于商品的好评评分
-
__init__
(db, brand_name=('vivo', 'oppo', 'mi', 'huawei', 'apple', 'SMARTISAN'))¶ 参数: - db – 一个 pymongo.MongoClient.db 的实例
- brand_name – 所研究的所有品牌名
-
brand_to_id
(brand)¶ 通过品牌名得到其下商品
参数: brand – list, 所研究的所有品牌名 返回: iteration, 遍历该品牌下所有商品(dictionary)的iteration
-
draw_final_score
(final_score)¶ 画散点图
参数: final_score – list(shape=(m,n), m为品牌数,n为评论数)
-
draw_scatter
(x, y, title, write_file_address)¶ 画散点图
参数: - x – 横轴
- y – 纵轴
- title – 图片标题
- write_file_address – 图片保存地址
-
id_to_each_score
(item_id)¶ 通过商品ID得到其评论情感评分
参数: item_id – string, 商品ID 返回: list, 该ID商品所有评论的情感评分
-
run
()¶ 运行函数 保存三种评分两两散点图与各品牌最终评分图
-
score_to_three_score
(score, fractile_parameter, good_comment_parameter)¶ 根据一种评分得到三个评分
参数: - score – 原始评分分数
- fractile_parameter – 分位数参数, (0,1)
- good_comment_parameter – 好评比例参数, (0,1)
返回: list(平均数-分位数-好评比例)
-
set_brand
(brand_name)¶ 更改品牌名称
参数: brand_name – list, 品牌名称
-
three_score_to_final_score
(scores, weight)¶ 根据三种评分得到最终评分
参数: - scores – 三种评分
- weight – 三种评分权重
返回: list(最终评分)
-
DB¶
生成 DB 示例以供其他模块使用
用法¶
生成数据库实例¶
from taobao_crawler.utils.db import DB
db_config = {'db_user': '', # 数据库用户名
'db_pass': '', # 数据库密码
'db_host': 'localhost', # 数据库地址
'db_port': 27017, # 数据库端口
'db_name': 'taobao'} # 数据库 collection 名称
mongo = DB(db_config)
与其他模块搭配使用¶
rate_crawler = RateCrawler(mongo.db)
关闭数据库连接¶
mongo.close()