当前位置:   article > 正文

python3+selenium爬取笔记本电脑详情信息_selenium可以爬取pc端数据吗

selenium可以爬取pc端数据吗

python3+selenium爬取购物商店

准备工作

# 用到的包
selenium #Web自动化测试工具
urllib #URL地址中查询参数进行编码
 xlwt  # 存储execl文件
 time # 加载数据缓冲时间
  • 1
  • 2
  • 3
  • 4
  • 5
  • selenium未安装的可通过以下方式安装

Linux:

 sudo pip3 install selenium
  • 1

Windows:

 python -m pip install selenium
  • 1
  • 浏览器驱动 需提前下载驱动
    chromedriver : 下载与浏览器对应版本
    谷歌浏览器
    geckodriver
    火狐浏览器

  • 添加到系统环境变量

  • 1.1) windows: 将解压后的可执行文件拷贝到Python安装目录的Scripts目录中
    windows查看python安装目录(cmd命令行):

    where python
    
    • 1
  • 1.2) Linux : 将解压后的文件拷贝到/usr/bin目录中

    sudo cp chromedriver /usr/bin/
    
    • 1

分析

url

这里我们比较一下搜索栏的url太过长?删减一下
在这里插入图片描述
经过测试发现url的规律

https://search.jd.com/Search?keyword=搜索的关键词
  • 1

xpath

获取需要的xpath
1.F12 点击左上角选中我们需要找的内容
F12
选中后的样式
搜索框
搜索框
然后我们选中被标亮的代码部分点击鼠标右键
搜索xpath
选择xpath —>copy xpath
拿取我们需要的xpath

//*[@id="key"]
  • 1

同理方法获取搜索按钮的xpath
搜索
搜索
搜索xpath
获取搜索的xpath

//*[@id="search"]/div/div[2]/button
  • 1

单个详情的url的xpath获取
在这里插入图片描述
单个电脑信息的url的xpath

//*[@id="J_goodsList"]/ul/li[1]/div/div[1]/a
  • 1

比较一下各个详情的xpath的区别

 //*[@id="J_goodsList"]/ul/li[2]/div/div[1]/a
//*[@id="J_goodsList"]/ul/li[1]/div/div[1]/a
  • 1
  • 2

总结出的xpath

//*[@id="J_goodsList"]/ul/li/div/div[1]/a
  • 1

xpath的语法 可以先了解一下xpath语法

//从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
选取属性

这里选取href属性

//*[@id="J_goodsList"]/ul/li/div/div[1]/a/@href
  • 1

=

需要获取的内容分这几种情况 京东价/有预售/待发布 已发布
代发布
预售
根据不同情况使用不同的xpath获取需要数据

# try 防止获取的数据不存在报错
# 型号
            model = self.driver.find_element_by_xpath('//div[@class="sku-name"]').text

            # 售价
            try:
                price = self.driver.find_element_by_xpath('//div[@class="dd"]/span[@class="p-price"]').text
            except Exception as error:
                # 不存在默认0000
                price = '0000'

            # 定金
            try:
                dj_price = self.driver.find_element_by_xpath(
                    '//div[@class="dd"]/span[@class="p-price dj-price"]').text
            except Exception as error:
                dj_price = '0000'

            # 预售
            try:
                ys_price = self.driver.find_element_by_xpath('//div[@class="dd"]/span[@class="p-price ys-price"]').text
            except Exception as error:
                ys_price = '0000'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

附完整代码

# -*- coding: UTF-8 -*-

"""
采用的是
python3.6

selenium + chromedriver + Firefox 浏览器 
selenium + geckodriver + Chrome   浏览器

均可使用

"""
from selenium import webdriver
from urllib import parse
import xlwt
import time


class JdSpider:
    def __init__(self):
        self.url = 'https://www.jd.com/'  # 主页面url
        self.word_url = 'https://search.jd.com/Search?keyword='  # 搜素  筛选后的url
        # 无界面浏览器
        # self.options=webdriver.FirefoxOptions()
        # self.options.add_argument('--headless')
        # self.driver=webdriver.Firefox(options=self.options)

        # 有页面浏览器
        # self.driver = webdriver.Chrome()
        #创建浏览器对象
        self.driver = webdriver.Firefox()
		# 输入你需要搜索的关键词
        self.word = input('请输入搜索:>>')

        # # 创建工作薄
        self.f = xlwt.Workbook()
        # 创建一个sheet
        self.sheet = self.f.add_sheet(self.word, cell_overwrite_ok=True)
        # # 型号 单价 预售 定金
        self.data = []
        # 定义每一列大小
        self.col1 = self.sheet.col(0)
        self.col1.width = 260 * 100
        self.col2 = self.sheet.col(1)
        self.col2.width = 260 * 20

        self.col3 = self.sheet.col(2)
        self.col3.width = 260 * 20

        self.col4 = self.sheet.col(3)
        self.col4.width = 260 * 20

    def get_html(self, url):
        # 打开网页
        self.driver.get(url=url)

    def crawl(self):
        self.get_html(url=self.url)

        # 1. 找到搜索框
        # 2.输入搜索的关键词
        self.driver.find_element_by_xpath('//*[@id="key"]').send_keys(self.word)
        # 3.找到搜索点击搜索
        self.driver.find_element_by_xpath('//*[@id="search"]/div/div[2]/button').click()

    def scroll_to_bottom(self):
        # 执行这段代码,会获取到当前窗口总高度
        js = "return action=document.body.scrollHeight"
        # 初始化现在滚动条所在高度为0
        height = 0
        # 当前窗口总高度
        new_height = self.driver.execute_script(js)

        while height < new_height:

            # 将滚动条调整至页面底部
            for i in range(height, new_height, 200):
                self.driver.execute_script('window.scrollTo(0, {})'.format(i))
                # 给予下拉数据缓冲时间
                time.sleep(0.5)
            height = new_height
            time.sleep(2)
            new_height = self.driver.execute_script(js)

    def search(self):

        self.crawl()
        # 地址栏中的url中字符串需要编码
        url_word = parse.quote(self.word)
        # 拼接url
        self.get_html(url=self.word_url + url_word)

        # 滚动下拉条
        self.scroll_to_bottom()

        li_list = self.driver.find_elements_by_xpath('//*[@id="J_goodsList"]/ul/li/div/div[1]/a')

        # 获取详情的url
        second_list = []
        for page_url in li_list:
            #获取url
            second_url = page_url.get_attribute('href')
            second_list.append(second_url)

        self.item(second_list)

    # 处理内层
    def item(self, second_list):
        # 遍历获取
        for item_url in second_list:
            self.get_html(url=item_url)
            # 获取需要数据
            # 使用 selenium爬取 xpath使用copy xpath的 
            # 型号
            model = self.driver.find_element_by_xpath('//div[@class="sku-name"]').text

            # 售价
            try:
                price = self.driver.find_element_by_xpath('//div[@class="dd"]/span[@class="p-price"]').text
            except Exception as error:
                # 不存在默认0000
                price = '0000'

            # 定金
            try:
                dj_price = self.driver.find_element_by_xpath(
                    '//div[@class="dd"]/span[@class="p-price dj-price"]').text
            except Exception as error:
                dj_price = '0000'

            # 预售
            try:
                ys_price = self.driver.find_element_by_xpath('//div[@class="dd"]/span[@class="p-price ys-price"]').text
            except Exception as error:
                ys_price = '0000'

            # 替换数据中¥ 
            data_list = [model, price.replace('¥', ''), dj_price.replace('¥', ''), ys_price.replace('¥', '')]

            self.data.append(data_list)
        # 保存
        self.save()

    def save(self):

        # 存储数据
        print(self.data)

        # 初始化第一行作为列名
        row0 = ['型号','单价','定金','预售']
        # # 写入第一行为列名
        for i in range(0, len(row0)):
            self.sheet.write(0, i, row0[i])  # 在第0行第一列

        # 同理剩下的数据按照上面的方式写入

        for i in range(len(self.data)):
            for j in range(len(self.data[i])):
                # 因为第0行为列名,所以这里下标i+1
                self.sheet.write(i + 1, j, self.data[i][j])  ##第i+1行第j列写入数据data[i][j]

        # 保存文件
        self.f.save(f'{self.word}.xls')

    def run(self):
        '''执行函数'''
        self.search()

        # 关闭浏览器
        self.driver.quit()


# 程序的执行入口
if __name__ == '__main__':
    jd = JdSpider()
    jd.run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177

在这里插入图片描述

写到最后这篇文章对你有帮助吗?在评论区留下你的困惑或你的见解,大家一起来交流吧!我们下期见!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/525662
推荐阅读
相关标签
  

闽ICP备14008679号