亚马逊评论爬虫+数据分析

爬取评论

做分析首先得有数据,数据是核心,而且要准确!

1、爬虫必要步骤,选好框架

2、开发所需数据

3、最后测试流程

这里我所选框架是selenium+request,很多人觉得selenium慢,确实不快,仅针对此项目我做过测试,相对于request要快,要方便一些!也可以用你们熟悉的框架,用的趁手就行!

最核心的要采用无浏览器模式,这样会快很多

安装浏览器对应webdriver版本

bash 复制代码
http://npm.taobao.org/mirrors/chromedriver/

获取评论数,评级数, 监控评论

·亚马逊产品评论分为5个等级,从1到5

bash 复制代码
    def get_review_summarys(self):

        # 解析评论星级
        def parse(site, asin, rating, html):
            # 解析评论星级
            selector = etree.HTML(html)
            title = select(selector, "//a[@data-hook='product-link']/text()", 0, None)
            if not title:
                return site, asin, self.parent_asin, None, None, None, None
            review_rating_count = select(selector, "//div[@data-hook='cr-filter-info-review-rating-count']/span/text()",0,
                                         None)
            if review_rating_count:
                review_rating_count = [s.strip() for s in review_rating_count.split("|")]
                rating_count = int(review_rating_count[0].split(" ")[0].replace(",", ""))
                review_count = int(review_rating_count[1].split(" ")[0].replace(",", ""))
                only_rating_count = rating_count - review_count
            else:
                rating_count = None
                review_count = None
                only_rating_count = None
            print(site, asin, rating, rating_count, review_count, only_rating_count, sep="\t")
            return site, asin, rating, rating_count, review_count, only_rating_count

        self.review_summarys = []
        run_successfully = 1
        star_map = {1: "one_star", 2: "two_star", 3: "three_star", 4: "four_star", 5: "five_star"}
        url_map = {"parent": "{}product-reviews/{}/?language={}&filterByStar={}&reviewerType=all_reviews"}
        try:
            for rating, star in star_map.items():
                index_url = 'https://www.amazon.com/'
                language = 'ref=cm_cr_arp_d_viewopt_sr?ie=UTF8'
                url_format = url_map.get(self.parent_asin,"{}product-reviews/{}/?language={}&filterByStar={}&reviewerType=all_reviews&formatType=current_format")
                self.re_url = url_format.format(index_url,self.asin, language, star)
                res = requests.get(self.re_url, headers=headers).text
                self.rating = rating
                review_summary = parse(self.site, self.asin, self.rating,res)
                self.review_summarys.append(review_summary)

        except Exception as err:
            print("请求中断:{}".format(err))
            run_successfully = 0
        finally:
            return run_successfully, self.review_summarys

获取评论内容

bash 复制代码
    def get_main_information(self):
        # 等待页面加载完毕
        while True:
            try:
                WebDriverWait(self.driver, 30).until(
                    EC.presence_of_element_located((By.ID, 'cm_cr-review_list'))
                )
                break
            except Exception as e:
                print(e)
                self.driver.refresh()
                continue
        us = self.driver.find_element_by_id("cm_cr-review_list")

        # 获取每页的全部评论信息
        text = self.driver.page_source
        selector = etree.HTML(text)
        self.review_detail = []
        try:
            reviews = selector.xpath("//div[@data-hook='review']")
            for review in reviews:
                review_id = select(review, "./@id", 0, "")
                customer = select(review, ".//span[@class='a-profile-name']/text()", 0, "")
                review_title = select(review, ".//*[@data-hook='review-title']/span/text()", 0, "")
                review_date = select(review, ".//span[contains(@*,'review-date')]/text()", 0, None)
                if review_date:
                    li = re.findall("\d+.\d+.\d+.\d+.", review_date)[0]
                    yyyy = re.findall('\d\d\d\d',li)[0]
                    mm = re.findall('年(\d+)',li)[0]
                    dd = re.findall('月(\d+)',li)[0]
                    review_date = "{}-{}-{}".format(yyyy, mm, dd)
                else:
                    review_date = None
                verified_purchase = select(review, ".//span[@data-hook='avp-badge']/text()", 0, "")
                review_body = select(review, ".//span[@data-hook='review-body']/span/text()")
                review_rating = select(review, ".//i[contains(@data-hook, 'review-star-rating')]/span/text()", 0, "")
                review_rating = int(float(review_rating.split(" ")[0].replace(",", "."))) if review_rating else None
                review_href = select(review, ".//a[@class='a-link-normal']/@href", 0, "")
                review_href = review_href if review_id in review_href else ""
                index_url = 'https://www.amazon.com/'
                review_url = index_url + review_href[1:] if review_href else ""
                asin = re.findall('ASIN=(.*)',review_url)[0]
                print(self.site,asin,self.product_name,review_id, customer, review_title, review_date,verified_purchase, review_body,
                      review_rating, review_url, sep="\n")
                print("----------------------------------------------------------------------------------------------")
                self.review_detail.append((self.site,asin,self.product_name,review_id, customer, review_title, review_date,verified_purchase,review_body,
                      review_rating, review_url))
        except Exception as e:
            print(e)
            
        # 判断是否还有下一页next_page
        try:
            WebDriverWait(self.driver, 5).until(
                EC.element_to_be_clickable((By.XPATH, '//li[@class = "a-last"]/a'))
            )
            self.next_page = us.find_element_by_xpath('.//li[@class = "a-last"]/a').get_attribute("href")

        except NoSuchElementException:
            self.driver.find_elements_by_xpath('//li[@class = "a-disabled a-last"]')
            self.next_page = None
            print("未有下一页")

        except TimeoutException:
            self.next_page = None
            self.driver.refresh()
bash 复制代码
- 数据存储方式建议大家使用mysql,如果只是测试玩玩就用csv或者excel

数据有了,下面我们开始分析,怎么分析呢?这里我用到是tableau-BI工具,要结合业务需求来选择工具,BI更适合公司开发业务,实现企业化!

1、每日星级变动分析:评论数,星级数

每天实时更新评级数,把爬虫放到服务器上写一个定时任务!

通过tableau展示可视化报表

2、评论监控

每日实时更新有变动的评论数,评级数

重新建表,把计算逻辑写到函数中,通过比对的方法实现变动

3、最后通过BI展示

定时任务

bash 复制代码
def get_ratings(h=7, m=30):
    while True:
        now = datetime.datetime.now()
        # print(now.hour, now.minute)
        if now.hour == h and now.minute == m:
            spider_main()
        # 每隔60秒检测一次
        time.sleep(60)

if __name__ == '__main__':
    # get_reviews()
    get_ratings()

本文章若对你有帮助,烦请点赞,收藏,关注支持一下!

各位的支持和认可就是我最大的动力!

相关推荐
sp_fyf_20241 小时前
【大语言模型】ACL2024论文-20 SCIMON:面向新颖性的科学启示机器优化
人工智能·深度学习·机器学习·语言模型·自然语言处理·数据挖掘
山海青风1 小时前
使用OpenAI进行自动化报告和数据解读
数据分析
CopyLower2 小时前
AI赋能电商:智能购物推荐、会员分类与商品定价的创新探索
人工智能·分类·数据挖掘
YONG823_API2 小时前
1688商品数据采集API的测试对接步骤分享(提供免费测试key)
开发语言·数据库·爬虫·python·数据挖掘
2401_881198792 小时前
图像增强夜视仪行业全面而深入的分析
数据分析
好看资源平台2 小时前
网络爬虫总结与未来方向
爬虫·python
AI完全体4 小时前
【AI战略思考12】调整战略目标,聚焦数据分析,学习德川家康,剩者为王
人工智能·学习·数据分析·数据科学·找工作
天才奇男子6 小时前
Python爬虫爬取网页小说
开发语言·爬虫·python
数据小爬虫@6 小时前
利用Python爬虫获取商品评论:技术与实践
开发语言·爬虫·python
ProtonBase7 小时前
分布式 Data Warebase - 构筑 AI 时代数据基石
大数据·数据库·数据仓库·人工智能·分布式·数据分析·数据库系统