利用selenium爬取京东商品信息存放到mongodb
利用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的更多相关文章
- selenium模块使用详解、打码平台使用、xpath使用、使用selenium爬取京东商品信息、scrapy框架介绍与安装
今日内容概要 selenium的使用 打码平台使用 xpath使用 爬取京东商品信息 scrapy 介绍和安装 内容详细 1.selenium模块的使用 # 之前咱们学requests,可以发送htt ...
- python爬虫——用selenium爬取京东商品信息
1.先附上效果图(我偷懒只爬了4页) 2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...
- 爬虫之selenium爬取京东商品信息
import json import time from selenium import webdriver """ 发送请求 1.1生成driver对象 2.1窗口最大 ...
- selenium+phantomjs爬取京东商品信息
selenium+phantomjs爬取京东商品信息 今天自己实战写了个爬取京东商品信息,和上一篇的思路一样,附上链接:https://www.cnblogs.com/cany/p/10897618. ...
- 爬虫—Selenium爬取JD商品信息
一,抓取分析 本次目标是爬取京东商品信息,包括商品的图片,名称,价格,评价人数,店铺名称.抓取入口就是京东的搜索页面,这个链接可以通过直接构造参数访问https://search.jd.com/Sea ...
- 爬虫系列(十三) 用selenium爬取京东商品
这篇文章,我们将通过 selenium 模拟用户使用浏览器的行为,爬取京东商品信息,还是先放上最终的效果图: 1.网页分析 (1)初步分析 原本博主打算写一个能够爬取所有商品信息的爬虫,可是在分析过程 ...
- Python爬虫-爬取京东商品信息-按给定关键词
目的:按给定关键词爬取京东商品信息,并保存至mongodb. 字段:title.url.store.store_url.item_id.price.comments_count.comments 工具 ...
- Scrapy实战篇(七)之Scrapy配合Selenium爬取京东商城信息(下)
之前我们使用了selenium加Firefox作为下载中间件来实现爬取京东的商品信息.但是在大规模的爬取的时候,Firefox消耗资源比较多,因此我们希望换一种资源消耗更小的方法来爬取相关的信息. 下 ...
- 八个commit让你学会爬取京东商品信息
我发现现在不用标题党的套路还真不好吸引人,最近在做相关的事情,从而稍微总结出了一些文字.我一贯的想法吧,虽然才疏学浅,但是还是希望能帮助需要的人.博客园实在不适合这种章回体的文章.这里,我贴出正文的前 ...
随机推荐
- magento 1.9 上传后图片前后台无法正常显示
1.上传后图片不显示,设置 允许 flash 2.保证php 执行是内存大小至少为为128M,多种方式设置,这里以init_set为例子,在index.php 加入下面一行代码,根据情况而定 ini_ ...
- Linux系统下安装Angular2开发环境(Ubuntu16.0和deepin)
说明下,以下过程都是在ubuntu16.0系统下,win系统环境下的安装过程更简单,基本上可以仿效此环境来,除了不用配置系统命令(win下自动可以),node安装是exe程序,一键安装.另外,这里面像 ...
- centos7系统优化定制
#!/bin/bash #author junxi by #this script is only for CentOS 7.x #check the OS platform=`uname -i` i ...
- 自己DIY出来一个JSON结构化展示器
说来也巧,这个玩意,一直都想亲手写一个,因为一直用着各种网上提供的工具,觉得这个还是有些用途,毕竟,后面的实现思路和原理不是太复杂,就是对json的遍历,然后给予不同节点类型以不同的展现风格. 我这次 ...
- CentOS7.5实践快速部署LAMP+Tomcat成功运行阿里云或者腾讯云
安装一定要按照顺序来 1 先安装JDK+TOMCAT 点击看这里 2 在安装LAMP 点击看这里 3 最关键的就是这里 LAMP+Tomcat整合 我们不用源码编译安装,而是使用yum命令来完成. ...
- CentOS7 搭建 SVN 服务器
CentOS7 搭建 SVN 服务器 介绍SVN: SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上 ...
- L1、L2范数理解
读了博主https://blog.csdn.net/a493823882/article/details/80569888的文章做简要的记录. 范数可以当作距离来理解. L1范数: 曼哈顿距离,是机器 ...
- WindowsDenfender
c:\Program Files\Windows Defender>MpCmdRun.exe -scan -scantype 3 -file "D:\手动更新病毒库" -Di ...
- MVC object htmlAttributes,IDictionary<string, object> htmlAttributes 写法
MVC object htmlAttributes:new {style="color:red",width="12px",height="10px& ...
- python之路——17
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.迭代器2.生成器3.内置函数 1.学习55个 2.带key的,max min filter map ...