利用selenium爬取京东商城的商品信息思路:
  1、首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构
  2、根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归。等待数据全部加载完成。
  3、创建下一页的函数去完成点击事件,获取下一页的数据
  4、首页处理就直接放在脚本运行就好了。
  5、将数据放到mongodb中 可以实现自己定义搜索内容,注意京东的页面数据最大为100页。 不完善的地方:
  1、每次都是利用sleep等待加载。浪费时间
  2、网速不好程序会因为没有获取到标签崩溃。
  3、获取下一页的方式可以改成发送请求的方式
  4、爬取一半重新爬取的数据重复的会重新覆盖数据库旧的数据。 在网速良好的情况下还是能正常的保证数据爬取
# <! -/* author by:daidai */>
# 京东商品信息爬取脚本
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from urllib.parse import urlencode
import mongodemo
import time driver = Chrome()
chrome_options = Options()
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # keyword = '电脑' # 京东搜索页的链接url = https://search.jd.com/Search?keyword=%E7%94%B5%E8%84%91&enc=utf-8&wq=%E7%94%B5%E8%84%91&pvid=53766f90843b4166be83aa2139402e65
# par = {'enc': 'utf-8', 'keyword': keyword, 'wq': keyword} # kw = urlencode(par) # url = 'https://search.jd.com/Search?' + kw
# 请求搜索页
# driver.get(url) # 等待页面加载
# driver.implicitly_wait(5) # 由于页面有懒加载效果,所以先获取页面的高度
# height = driver.execute_script("return document.body.clientHeight") # 等待页面加载完成
# driver.implicitly_wait(7) # 由分析知道,页面有懒加载效果,得知一页数据为60条
def get_data():
# 获取包裹商品信息的大标签ul
ul = driver.find_element_by_class_name('gl-warp')
# 商品列表 在标签li class = "gl-item"中, 完整一页一共60个
items = ul.find_elements_by_class_name('gl-item') if len(items) == 60:
return items
# 如果没有获取到60条就递归调用,直到获取该页完整的数据
else:
return get_data() def parser_data(items):
for item in items:
# 商品的详细信息都在标签div class=p-img 下 它的下面的a标签下是图片的信息
link = item.find_element_by_css_selector(".p-img a").get_attribute("href")
# 图片地址
img = item.find_element_by_css_selector(".p-img a img").get_attribute("src")
# 因为在图片中也使用了懒加载,所以做判断
if not img:
img = item.find_element_by_css_selector(".p-img a img").get_attribute("data-lazy-img")
img = "https:" + img
# 商品价格
price = item.find_element_by_css_selector(".p-price i").text
# 商品的标题
title = item.find_element_by_css_selector(".p-name a em").text
# 店家名
trade_name = item.find_element_by_css_selector(".p-shop a").text
# 评论数
commit = item.find_element_by_css_selector(".p-commit").text
data = {"link": link, "img": img, "title": title, "shop_name": trade_name, "commit": commit, "price": price}
# 存到数据库
mongodemo.insert_data(data) def get_next_page():
# 获取下一页的数据,获取标签
next_page = driver.find_element_by_partial_link_text("下一页")
next_page.click() # 实现点击事件,跳转到下一页
# 滑动屏幕
driver.execute_script("""
window.scrollTo({
top: %s,
behavior: "smooth"
});""" % height)
time.sleep(1) # 等待页面加载
items = get_data()
parser_data(items) if __name__ == '__main__':
keyword = input('请输入要查找的信息:')
par = {'enc': 'utf-8', 'keyword': keyword, 'wq': keyword}
kw = urlencode(par)
url = 'https://search.jd.com/Search?' + kw
driver.get(url)
driver.implicitly_wait(10)
height = driver.execute_script("return document.body.clientHeight")
driver.implicitly_wait(10)
# 首页的处理 第一页的信息爬取
driver.execute_script("""
window.scrollTo({
top: %s,
behavior: "smooth"
});""" % height)
items = get_data()
parser_data(items) for i in range(8):
get_next_page() time.sleep(30)
driver.close() 将数据放到mongodb数据库中
from pymongo import MongoClient

table = None
c = None def connect_server():
global table, c
c = MongoClient('mongodb://root:root@127.0.0.1:27017') # 获取连接
table = c['jd']['data'] def insert_data(data):
if not table:
connect_server()
table.insert(data) def close():
c.close()

 

利用selenium爬取京东商品信息存放到mongodb的更多相关文章

  1. selenium模块使用详解、打码平台使用、xpath使用、使用selenium爬取京东商品信息、scrapy框架介绍与安装

    今日内容概要 selenium的使用 打码平台使用 xpath使用 爬取京东商品信息 scrapy 介绍和安装 内容详细 1.selenium模块的使用 # 之前咱们学requests,可以发送htt ...

  2. python爬虫——用selenium爬取京东商品信息

    1.先附上效果图(我偷懒只爬了4页)  2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...

  3. 爬虫之selenium爬取京东商品信息

    import json import time from selenium import webdriver """ 发送请求 1.1生成driver对象 2.1窗口最大 ...

  4. selenium+phantomjs爬取京东商品信息

    selenium+phantomjs爬取京东商品信息 今天自己实战写了个爬取京东商品信息,和上一篇的思路一样,附上链接:https://www.cnblogs.com/cany/p/10897618. ...

  5. 爬虫—Selenium爬取JD商品信息

    一,抓取分析 本次目标是爬取京东商品信息,包括商品的图片,名称,价格,评价人数,店铺名称.抓取入口就是京东的搜索页面,这个链接可以通过直接构造参数访问https://search.jd.com/Sea ...

  6. 爬虫系列(十三) 用selenium爬取京东商品

    这篇文章,我们将通过 selenium 模拟用户使用浏览器的行为,爬取京东商品信息,还是先放上最终的效果图: 1.网页分析 (1)初步分析 原本博主打算写一个能够爬取所有商品信息的爬虫,可是在分析过程 ...

  7. Python爬虫-爬取京东商品信息-按给定关键词

    目的:按给定关键词爬取京东商品信息,并保存至mongodb. 字段:title.url.store.store_url.item_id.price.comments_count.comments 工具 ...

  8. Scrapy实战篇(七)之Scrapy配合Selenium爬取京东商城信息(下)

    之前我们使用了selenium加Firefox作为下载中间件来实现爬取京东的商品信息.但是在大规模的爬取的时候,Firefox消耗资源比较多,因此我们希望换一种资源消耗更小的方法来爬取相关的信息. 下 ...

  9. 八个commit让你学会爬取京东商品信息

    我发现现在不用标题党的套路还真不好吸引人,最近在做相关的事情,从而稍微总结出了一些文字.我一贯的想法吧,虽然才疏学浅,但是还是希望能帮助需要的人.博客园实在不适合这种章回体的文章.这里,我贴出正文的前 ...

随机推荐

  1. [R] [Johns Hopkins] R Programming 作業 Week 2 - Air Pollution

    Introduction For this first programming assignment you will write three functions that are meant to ...

  2. Yii2 设计模式——设计模式简介

    我们首先来思考一个问题:作为工程师,我们的价值是什么? 笔者认为是——解决用户问题. 我们的任何知识和技能,如果不能解决特定的问题,那么就是无用的屠龙之术:我们的任何经验,如果不能对解决新的问题有用, ...

  3. SQL语句整理

  4. python2和3的区别

    一.默认编码 2:ascii 3:utf-8 二.数字 python3无long

  5. 内存泄漏学习案例-1-ArrayList

    解决 内存泄漏 于是赶快登陆探测服务器,首先是 top free df 三连,结果还真发现了些异常. 我们的探测进程 CPU 占用率特别高,达到了 900%. 我们的 Java 进程,并不做大量 CP ...

  6. elasticSearch 2.3 delete-by-query plugin

    The delete-by-query plugin adds support for deleteing all of the documents which match the specified ...

  7. DOM知识点总结

    今天简单整理了一下js三部曲之DOM部分的内容,二话不说先上笔记: 1.什么是DOM? Document Object Model,即文档对象模型,它是让JavaScript能够操作html和xml的 ...

  8. CentOS 7系统初始化

    1. 升级系统 $ yum -y update 2.SELinux设置: 禁用 $ vi /etc/selinux/config 修改 SELINUX=disabled

  9. 微信小程序布局

    尺寸单位与设计原则 首先,我们现在页面中引入一张图片    但是实际上,这个图片的大小是32*18的,之所以会显示这么大,是因为image组件默认的宽度为300px,默认的高度为225px,如果我们需 ...

  10. Exp3 免杀原理和实践

    一.基础问题回答 1.杀软是如何检测出恶意代码的? (1)基于特征码的检测 特征码是能识别一个程序是一个病毒的一段不大于64字节的特征串.如果一个可执行文件包含这样的特征码则被杀毒软件检测为是恶意代码 ...