抓取某东的TT购买记录分析TT购买趋势

 最近学习了一些爬虫技术,想做个小项目检验下自己的学习成果,在逛某东的时候,突然给我推荐一个TT的产品,点击进去浏览一番之后就产生了抓取TT产品,然后进行数据分析,看下那个品牌的TT卖得最好。

本文通过selenium抓取TT信息,存入到mongodb数据库中。

抓取TT产品信息

TT产品页面的连接是upload/201901101640336628.png" title="condom" alt="condom" style="margin: 0px; padding: 0px; border: 0px; max-width: 800px; height: auto;" />
通过上图可以看到一个TT产品信息对应的源代码是一个class为gl-item的li节点<li class='gl-item'>。li节点中data-sku属性是产品的ID,后面抓取产品的评论信息会用到,brand_id是品牌ID。class为p-price的div节点对应的是TT产品的价格信息。class为p-comment的div节点对应的是评论总数信息。

开始使用requests是总是无法解析到TT的价格和评论信息,最后适应selenium才解决了这个问题,如果有人知道怎么解决这问题,望不吝赐教。

下面介绍抓取TT产品评论信息。

点击一个TT产品,会跳转到产品详细页面,点击“商品评论”,然后勾选上“只看当前商品评价”选项(如果不勾选,就会看到该系列产品的评价)就会看到商品评论信息,我们用开发者工具看下如果抓取评论信息。
condom
如上图所示,在开发者工具中,点击Network选项,就会看到“https://club.jd.com/discussion/getSkuProductPageImageCommentList.action?productId=3521615&isShadowSku=0&callback=jQuery6014001&page=2&pageSize=10&_=1547042223100” 的链接,这个链接返回的是json数据。其中productId就是TT产品页面的data-sku属性的数据。page参数是第几页评论。返回的json数据中,content是评论数,createTime是下单时间。

代码如下:

 def parse_product(page,html):     doc = pq(html)     li_list = doc('.gl-item').items()     for li in li_list:         product_id = li('.gl-i-wrap').attr('data-sku')         brand_id = li('.gl-i-wrap').attr('brand_id')         time.sleep(get_random_time())         title = li('.p-name').find('em').text()         price_items = li('.p-price').find('.J_price').find('i').items()         price = 0         for price_item in price_items:             price = price_item.text()             break         total_comment_num = li('.p-commit').find('strong a').text()         if total_comment_num.endswith("万+"):             print('总评价数量:' + total_comment_num)             total_comment_num = str(int(float(total_comment_num[0:len(total_comment_num) -2]) * 10000))             print('转换后总评价数量:' + total_comment_num)         elif total_comment_num.endswith("+"):             total_comment_num = total_comment_num[0:len(total_comment_num) - 1]         condom = {}         condom["product_id"] = product_id         condom["brand_id"] = brand_id         condom["condom_name"] = title         condom["total_comment_num"] = total_comment_num         condom["price"] = price         comment_url = 'https://club.jd.com/comment/skuProductPageComments.action?callback=fetchJSON_comment98vv117396&productId=%s&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'         comment_url = comment_url %(product_id)         response = requests.get(comment_url,headers = headers)         if response.text == '':             for i in range(0,10):                 time.sleep(get_random_time())                 try:                     response = requests.get(comment_url, headers=headers)                 except requests.exceptions.ProxyError:                     time.sleep(get_random_time())                     response = requests.get(comment_url, headers=headers)                 if response.text:                     break                 else:                     continue         text = response.text         text = text[28:len(text) - 2]         jsons = json.loads(text)         productCommentSummary = jsons.get('productCommentSummary')         # productCommentSummary = response.json().get('productCommentSummary')         poor_count = productCommentSummary.get('poorCount')         general_count = productCommentSummary.get('generalCount')         good_count = productCommentSummary.get('goodCount')         comment_count = productCommentSummary.get('commentCount')         poor_rate = productCommentSummary.get('poorRate')         good_rate = productCommentSummary.get('goodRate')         general_rate = productCommentSummary.get('generalRate')         default_good_count = productCommentSummary.get('defaultGoodCount')         condom["poor_count"] = poor_count         condom["general_count"] = general_count         condom["good_count"] = good_count         condom["comment_count"] = comment_count         condom["poor_rate"] = poor_rate         condom["good_rate"] = good_rate         condom["general_rate"] = general_rate         condom["default_good_count"] = default_good_count         collection.insert(condom)          comments = jsons.get('comments')         if comments:             for comment in comments:                 print('解析评论')                 condom_comment = {}                 reference_time = comment.get('referenceTime')                 content = comment.get('content')                 product_color = comment.get(
                        
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信