selenium_获取京东商品价格

import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from pymongo import MongoClient
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import urllib

options = Options()

# 开启无界面模式
options.add_argument('--headless')
web = Chrome(options=options)


def get_save_goods():
    # 输入要搜索的商品名称
    good_name = input('请输入你要爬取的商品名称:')
    # 编码加入到url中
    good_name_unicode = urllib.parse.quote(good_name)
    url = f'https://search.jd.com/Search?keyword={good_name_unicode}'
    web.get(url)
    # 设置等待时间
    WAIT = WebDriverWait(web, 10)

    while True:
        # 新的商品页等待3秒
        time.sleep(3)
        # 隐式等待 根据下面要查询的元素 10s未找到就报错
        # web.implicitly_wait(10)

        # 获取爬取的页数
        page = WAIT.until(EC.element_to_be_clickable((By.XPATH, '//span/a[@class="curr"]'))).text
        print(f'开始爬取第{page}页')

        # 获取商品信息的列表
        li_lists = web.find_elements(By.XPATH, '//div[@id="J_goodsList"]/ul/li')
        print(len(li_lists))

        # 用xpath获取各个商品的信息
        for li_list in li_lists:

            name = WAIT.until(EC.element_to_be_clickable(li_list.find_element(By.XPATH, './/div[@class="p-name p-name-type-2"]/a/em'))).text.replace('\n', '')

            join_name = ''.join(name)

            link = WAIT.until(EC.element_to_be_clickable(li_list.find_element(By.XPATH, './/div[@class="p-name p-name-type-2"]/a'))).get_attribute('href')

            price = WAIT.until(EC.element_to_be_clickable(li_list.find_element(By.XPATH, './/div[@class="p-price"]//i'))).text


            try: # 有的商品没有显示厂家
                shop = WAIT.until(EC.element_to_be_clickable(li_list.find_element(By.XPATH, './/div[@class="p-shop"]//a'))).text
            except:
                shop = '厂家配送'

            comment = WAIT.until(EC.element_to_be_clickable(li_list.find_element(By.CSS_SELECTOR, '.p-commit a'))).text
            data = {
                '商品名称': name,
                '商品链接': link,
                '价格': price,
                '商店名称': shop,
                '评论': comment
            }
            # 储存到mongodb
            print(data)
            client = MongoClient(host='localhost', port=27017)
            # 库
            db = client['jd']
            # 表
            collections = db[good_name]
            # 插入数据
            collections.insert_one(data)

        # 爬完5页停止
        if int(page) == 5:
            break
        #     点击下一页
        js = WAIT.until(EC.element_to_be_clickable(web.find_element(By.XPATH, '//a[@class="pn-next"]')))
        js.click()

        # 等待加载完毕
        time.sleep(2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值