MSIPO技术圈 首页 IT技术 查看内容

python写爬虫爬取京东商品信息

2024-03-25

在这里插入图片描述

工具库

爬虫有两种方案:

  • 第一种方式是使用request模拟请求,并使用bs4解析respond得到数据。
  • 第二种是使用selenium和无头浏览器,selenium自动化操作无头浏览器,由无头浏览器实现请求,对得到的数据进行解析。

第一种方案部署简单,效率高,对于静态页面效果较好,对于动态页面效果较差。【可以理解为直接与服务器对接,申请什么数据完全由你自己来决定】

对于网页来说,可以分为静态网页和动态网页,二者的区别是静态网页是对于你的申请切切实实存在一个html网页文件,将这个文件发给你,你浏览器进行渲染。而动态网页则是存在一个服务器框架,处理你的请求,临时组合成一个html网页发给你,你浏览器进行渲染,你得到的是服务器框架的产物。
在这里插入图片描述

因此网页的数据来源也可以分为:
1、静态网页内的,
2、通过Ajax接口申请的,例如商品的评价数量,加载网页时不随网页一块儿得到,而是额外申请
3、通过JS脚本运行+Ajax接口申请的,例如商品的具体评价,只有你点评论栏,JS脚本才会向服务器申请数据

第二种方案部署稍微麻烦,需要安装无头浏览器,但是爬取效果较好,因为是真实的浏览器申请,selenium是模拟真人进行操作,对于反爬虫效果较好。
本文使用的是第一种,所需的工具库:
Python库:
Beautifulsoup
request
json

方法:

1、登录京东,获取登录cookie
2、搜索,得到搜索链接
3、使用request向搜索链接发送请求,得到respond
4、使用bs4解析respond
5、定位想要的数据所在的tag
6、对于一些动态数据,在浏览器开发者工具的network中找到相应的服务器地址,使用request模拟请求,并使用json解析服务器的respond

代码

import requests, json
from bs4 import BeautifulSoup


# 基类,后续可以在此之上扩展
class AbstractWebPage:
    def __init__(self, cookie, use_cookie=True):
        if use_cookie:
            self.headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                              'Chrome/80.0.3987.149 Safari/537.36',
                'cookie': cookie}
        else:
            self.headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                              'Chrome/80.0.3987.149 Safari/537.36'
            }
        self.sess = requests.session()
        self.sess.headers.update(self.headers)


# 目录类,用来表示搜索结果
class Content(AbstractWebPage):
    def __init__(self, cookie, keyword, end_page):
        super(Content, self).__init__(cookie)
        start_url = 'https://search.jd.com/Search?keyword=' + keyword + '&enc=utf-8&wq=' + keyword
        self.url_list = [start_url + '&page=' + str(j) for j in range(1, end_page + 1)]
        self.end_page = end_page

    def print(self):
        print(self.url_list, sep='\n')

    def get_item_info(self):
        item_pages_list = []
        with open("good_info.txt", 'w', encoding='utf-8') as f:
            f.write("产品名称" + '\t' + '价格' + '\t' + '销量' + '\t' '店铺' + '\n')
            f.write("*" * 50 + '\n')
            for url in self.url_list:
                res = self.sess.get(url)
                res.encoding = 'utf-8'
                res = res.text
                # 定位搜索结果主体,并获取所有的商品的标签
                soup = BeautifulSoup(res, 'html.parser').select('#J_goodsList > ul')
                good_list = soup[0].select('[class=gl-i-wrap]')
                # 循环获取所有商品信息
                for temp in good_list:
                    # 获取名称信息
                    name_div = temp.select_one('[class="p-name p-name-type-2"]')
                    good_info = name_div.text.strip() + '\t'

                    # 价格信息
                    price_div = temp.select_one('[class=p-price]')
                    good_info += price_div.text.strip() + '\t'

                    # 评价信息
                    comment_div = temp.select_one('[class=p-commit]').find('strong').find('a')
                    comment_url = comment_div.get('href')
                    good_id = comment_url.replace('//item.jd.com/', '').replace('.html#comment', '')
                    # 评价信息没有在主页面内,而是需要另外发送GET获取,服务器地址如下
                    # 这里面的uuid是唯一标识符,如果运行程序发现报错或者没有得到想要的结果
                    # commit_start_url = f'https://api.m.jd.com/?appid=item-v3&functionId' \
                    #                    '=pc_club_productCommentSummaries&client=pc&clientVersion=1.0.0&t' \
                    #                    f'=1711091114924&referenceIds={good_id}&categoryIds=9987%2C653%2C655' \
                    #                    '&loginType=3&bbtf=&shield=&uuid=181111935.1679801589641754328424.1679801589' \
                    #                    '.1711082862.1711087044.29'
                    commit_start_url = f'https://api.m.jd.com/?appid=item-v3&functionId' \
                                       '=pc_club_productCommentSummaries&client=pc&clientVersion=1.0.0&t' \
                                       f'=1711091114924&referenceIds={good_id}&categoryIds=9987%2C653%2C655'
                    # 发送请求,得到结果
                    comment_res = self.sess.get(commit_start_url)
                    # 编码方式是GBK国标编码
                    comment_res.encoding = 'gbk'
                    comment_res_json = comment_res.json()

                    # 解析得到评论数量
                    good_info += comment_res_json['CommentsCount'][0]['CommentCountStr'] + '\t'

                    # 店铺信息
                    shop_div = temp.select_one('[class=p-shop]')
                    good_info += shop_div.get_text().strip() + '\t'
                    f.write(good_info + '\n')
                    f.write("*" * 50 + '\n')
            f.close()

        return item_pages_list


if __name__ == "__main__":
    # cookie,用于验证登录状态,必须要有cookie,否则京东会提示网络繁忙请重试
    # 获取方法:使用浏览器登录过后按F12,点击弹出界面中最上方的network选项,name栏里面随便点开一个,拉到最下面就有cookie,复制到cookie.txt中
    # 注意,不要换行,前后不要有空格,只需要复制cookie的值,不需要复制“cookie:”这几个字符
    # 上面的看不懂的话,看这个:https://blog.csdn.net/qq_46047971/article/details/121694916
    # 然后就可以运行程序了
    cookie_str = ''
    with open('cookie.txt') as f:
        cookie_str = f.readline()
    
    # 输入cookie,关键词,输入结束页数
    content_page = Content(cookie_str, '手机', 2)
    content_page.print()

    urls = content_page.get_item_info()

相关阅读

热门文章

    手机版|MSIPO技术圈 皖ICP备19022944号-2

    Copyright © 2024, msipo.com

    返回顶部