关于scrapy:scrapy爬虫框架和selenium的使用对优惠券推荐网站数据LDA文本挖掘

7次阅读

共计 3943 个字符,预计需要花费 10 分钟才能阅读完成。

原文链接:http://tecdat.cn/?p=12203


介绍

每个人都喜爱省钱。咱们都试图充分利用咱们的资金,有时候这是最简略的事件,能够造成最大的不同。长期以来,优惠券始终被带到超市拿到折扣,但应用优惠券从未如此简略,这要归功于 Groupon。

Groupon 是一个优惠券举荐服务,能够在您左近的餐馆和商店播送电子优惠券。其中一些优惠券可能十分重要,特地是在打算小组活动时,因为折扣能够高达 60%。

数据

这些数据是从 Groupon 网站的纽约市区域取得的。网站的布局分为所有不同 groupon 的专辑搜寻,而后是每个特定 groupon 的深度页面。网站外观如下所示:

两个页面的布局都不是动静的,所以建设了一个自定义 scrapy,以便疾速浏览所有的页面并检索要剖析的信息。然而,评论,重要的信息,通过 JavaScript 出现和加载。Selenium 脚本应用从 scrapy 获取的 groupons 的 URL,本质上模拟了人类点击用户正文局部中的“next”按钮。

for url in url_list.url[0:50]:    try:        driver.get(url)        time.sleep(2)        #Close Any Popup That Occurs#        # if(driver.switch_to_alert()):        try:            close = driver.find_element_by_xpath('//a[@id="nothx"]')            close.click()        except:            pass        time.sleep(1)        try:            link = driver.find_element_by_xpath('//div[@id="all-tips-link"]')            driver.execute_script("arguments[0].click();", link)            time.sleep(2)        except:            next        i = 1        print(url)        while True:            try:                time.sleep(2)                print("Scraping Page:" + str(i))                reviews = driver.find_elements_by_xpath('//div[@class="tip-item classic-tip"]')                next_bt = driver.find_element_by_link_text('Next')                for review in reviews[3:]:                    review_dict = {}                    content = review.find_element_by_xpath('.//div[@class="twelve columns tip-text ugc-ellipsisable-tip ellipsis"]').text                    author = review.find_element_by_xpath('.//div[@class="user-text"]/span[@class="tips-reviewer-name"]').text                    date = review.find_element_by_xpath('.//div[@class="user-text"]/span[@class="reviewer-reviewed-date"]').text                    review_dict['author'] = author                    review_dict['date'] = date                    review_dict['content'] = content                    review_dict['url'] = url                    writer.writerow(review_dict.values())                i += 1                 next_bt.click()            except:                break    except:        nextcsv_file.close()driver.close()

从每个组中检索的数据如下所示。

Groupon 题目 

分类信息

交易功能位置

总评分数网址

作者日期

评论网址

大概有 89,000 个用户评论。从每个评论中检索的数据如下所示。

print(all_groupon_reviews[all_groupon_reviews.content.apply(lambda x: isinstance(x, float))])indx = [10096]all_groupon_reviews.content.iloc[indx]            author       date content  \10096  Patricia D. 2017-02-15     NaN   15846       Pat H. 2016-09-24     NaN   19595      Tova F. 2012-12-20     NaN   40328   Phyllis H. 2015-06-28     NaN   80140     Andre A. 2013-03-26     NaN                                                    url  year  month  day  10096  https://www.groupon.com/deals/statler-grill-9  2017      2   15  15846         https://www.groupon.com/deals/impark-3  2016      9   24  19595   https://www.groupon.com/deals/hair-bar-nyc-1  2012     12   20  40328     https://www.groupon.com/deals/kumo-sushi-1  2015      6   28  80140  https://www.groupon.com/deals/woodburybus-com  2013      3   26  

探索性数据分析

一个乏味的发现是在过来的几年里,群体的应用曾经大大增加了。咱们通过查看评论提供的日期来发现这一点。看上面的图像,其中 x 轴示意月 / 年和 y 轴,示意计数,这个论断变得显著。最初的小幅下滑是因为过后的一些小组可能是季节性的。

一个乏味的发现是在过来的几年里,群体的应用曾经大大增加了。咱们通过查看评论提供的日期来发现这一点。看上面的图像,其中 x 轴示意月 / 年和 y 轴,示意计数。最初的小幅下滑是因为过后的一些小组可能是季节性的。

pie_chart_df = Groupons.groupby('categories').agg('count')plt.rcParams['figure.figsize'] = (8,8)sizes = list(pie_chart_df.mini_info)labels = pie_chart_df.indexplt.pie(sizes, shadow=True, labels = labels, autopct='%1.1f%%', startangle=140)# plt.legend(labels, loc="best")plt.axis('equal')

最初,因为大部分数据是通过文本:价格(原价),导出了一个正则表达式来解析价格信息,以及它们提供的交易数量。该信息显示在以下条形图中:

objects = list(offer_counts.keys())y = list(offer_counts.values())tst = np.arange(len(y))plt.bar(tst,y, align = 'center')plt.xticks(tst, objects)plt.ylabel('Total Number of Groupons')plt.xlabel('Different Discounts Offers')plt.show()

plt.ylabel('Number of Offerings')plt.xticks(ind, ('Auto', 'Beauty', 'Food', 'Health', 'Home', 'Personal', 'Things'))plt.xlabel('Category of Groupon')plt.legend((p0[0], p1[0], p2[0], p3[0], p4[0], p5[0], p6[0], p7[0], p10[0]), ('0', '1', '2', '3', '4', '5', '6', '7', '10'))

sns.violinplot(data = savings_dataframe)

最初,利用用户评论数据生成一个文字云:

plt.rcParams['figure.figsize'] = (20,20)wordcloud = WordCloud(width=4000, height=2000, max_words=150, background_color='white').generate(text)plt.imshow(wordcloud, interpolation='bilinear')plt.axis("off")

主题建模

为了进行主题建模,应用的两个最重要的软件包是_gensim_和_spacy_。创立一个语料库的第一步是删除所有停用词,如“,”等。最初发明 trigrams。

抉择的模型是 Latent Dirichlet Allocation,因为它可能辨别来自不同文档的主题,并且存在一个能够清晰无效地将后果可视化的包。因为该办法是无监督的,因而必须当时抉择主题数量,在模型的 25 次连续迭代中最优数目为 3。后果如下:

下面的可视化是将主题投影到两个组件上,其中类似的主题会更靠近,而不类似的主题会更远。左边的单词是组成每个主题的单词,lambda 参数管制单词的排他性。0 的 lambda 示意每个主题四周的最排挤的单词,而 1 的 lambda 示意每个主题四周的最频繁的单词。

第一个话题代表服务的品质和接待。第二个话题有形容锤炼和身材流动的词语。最初,第三个话题有属于食品类的词语。

论断

主题建模是无监督学习的一种模式,这个我的项目的范畴是简要地查看在根底词语背地发现模式的性能。尽管咱们认为咱们对某些产品 / 服务的评论是举世无双的,然而这个模型分明地表明,实际上,某些词汇在整个人群中被应用。

正文完
 0