python爬取当当网商品评论

python爬取当当网商品评论

本案例获取某鞋评论作为例

案例目的:

通过爬取当当网商品评价,介绍通过结合jsonpath和正则表达式获取目标数据的方法。

代码功能:

输入爬取的页数,自动下载保存每页的的评价以及对应用户昵称。

1.找到目标的url

在这里插入图片描述

2.检查响应结果

在这里插入图片描述
由上图,我们发现:评论的内容在json文件中的html文件之下。

当我们展开Show more时,可以找到评论内容。

在这里插入图片描述

那么问题来了,如何提取评论内容呢?

1.既然,节点html对应的数据为html格式,我们是否可以通过xpath的方法来提取里面的内容。答案是不可以,我经过调试之后,无法通过xpath进行提取。

2.我们可以通过jsonpath将html中的内容全部提取出来,然后再通过正则表达式将我们需要的商品评论和用户昵称提取出来。经过调试,答案是可以的。

3.找到提取方法后,还需要找到翻页的url规律。

找到前三页评论的url如下:

http://product.dangdang.com/index.php?r=comment%2Flist&productId=1412059069&categoryPath=58.65.03.03.00.00&mainProductId=1412059069&mediumId=21&pageIndex=1&sortType=1&filterType=1&isSystem=1&tagId=0&tagFilterCount=0&template=cloth
http://product.dangdang.com/index.php?r=comment%2Flist&productId=1412059069&categoryPath=58.65.03.03.00.00&mainProductId=1412059069&mediumId=21&pageIndex=2&sortType=1&filterType=1&isSystem=1&tagId=0&tagFilterCount=0&template=cloth&long_or_short=short
http://product.dangdang.com/index.php?r=comment%2Flist&productId=1412059069&categoryPath=58.65.03.03.00.00&mainProductId=1412059069&mediumId=21&pageIndex=3&sortType=1&filterType=1&isSystem=1&tagId=0&tagFilterCount=0&template=cloth&long_or_short=short

对比发现:不同页url的不同之处,在于参数pageindex。第一页对应1,第二页为2… … 而参数long_or_short=short对结果无影响,因此不做考虑。

所有问题分析完毕,上代码:

import requests
import jsonpath
import re
import json

if __name__ == '__main__':
    # 输入要爬取的页数
    pages = int(input('请输入要爬取的页数:'))
    for i in range(pages):
        page = i + 1
        # 确认目标的url
        url = f'http://product.dangdang.com/index.php?r=comment%2Flist&productId=1412059069&categoryPath=58.65.03.03.00.00&mainProductId=1412059069&mediumId=21&pageIndex={page}&sortType=1&filterType=1&isSystem=1&tagId=0&tagFilterCount=0&template=cloth'
        # 构造请求头参数
        headers = {
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36',
            'Referer':'http://product.dangdang.com/1412059069.html',
            'Cookie':'from=460-5-biaoti; order_follow_source=P-460-5-bi%7C%231%7C%23www.baidu.com%252Fother.php%253Fsc.060000jRtGgkBd47ECAxHUxBlqwLkfBJsl8lSLtmm9Zl27Qa_kZyOm2Qg_lyRgkRd4vKD9uWt%7C%230-%7C-; ddscreen=2; __permanent_id=20210304204636997189494350346254347; __visit_id=20210304204637001245338343220621735; __out_refer=1614861997%7C!%7Cwww.baidu.com%7C!%7C%25E5%25BD%2593%25E5%25BD%2593%25E7%25BD%2591; __ddc_15d_f=1614861997%7C!%7C_utm_brand_id%3D11106; dest_area=country_id%3D9000%26province_id%3D111%26city_id%3D0%26district_id%3D0%26town_id%3D0; pos_0_end=1614862009963; __ddc_1d=1614862062%7C!%7C_utm_brand_id%3D11106; __ddc_24h=1614862062%7C!%7C_utm_brand_id%3D11106; __ddc_15d=1614862062%7C!%7C_utm_brand_id%3D11106; pos_9_end=1614862078563; ad_ids=4343831%2C3554365%7C%233%2C2; secret_key=f097eea219c17c155499399cb471dd5a; pos_1_start=1614863547245; pos_1_end=1614863547264; __rpm=%7Cp_1412059069.029..1614863548625; __trace_id=20210304211706253212636290464425201'
        }
        # 发送请求,获取响应
        response = requests.get(url,headers=headers)
        # 响应类型为json类型
        py_data = response.json()
        # 解析数据,提取数据.
        html_data = jsonpath.jsonpath(py_data,'$..html')[0]
        # 通过正则表达式,提取html数据中的用户昵称和评论
        comment_list = re.findall(r'<div class="describe_detail">\s*<span>(.*?)</span>\s*</div>',html_data)
        nickname_list = re.findall(r'alt="(.*?)"',html_data)
        # 将数据放进字典
        for i in range(len(comment_list)):
            dict_ = {}
            dict_[nickname_list[i]] = comment_list[i]
            # 将字典转换成json格式
            json_data = json.dumps(dict_,ensure_ascii=False)+',\n'
            # 将数据保存到本地
            with open('当当网商品评价.json','a',encoding='utf-8')as f:
                f.write(json_data)

爬取了两页,每一页对应10个数据。

执行结果如下:

在这里插入图片描述

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜的乄第七章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值