利用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让你学会爬取京东商品信息
我发现现在不用标题党的套路还真不好吸引人,最近在做相关的事情,从而稍微总结出了一些文字.我一贯的想法吧,虽然才疏学浅,但是还是希望能帮助需要的人.博客园实在不适合这种章回体的文章.这里,我贴出正文的前 ...
随机推荐
- bookmarks
嵌入式/硬件/计算机原理 PCI ID的分配 ARM汇编指令介绍 (连续4篇) https://blog.csdn.net/makethyme/article/details/1641413https ...
- day01代码
1. 使用while循环打印1,2,3,4,5,7,8,9 # 使用while循环打印1,2,3,4,5,7,8,9 count = 0 while count < 10: count += 1 ...
- MySQL必知必会 前10章学习笔记
1. 在使用用户名和密码登陆MySQL数据库之后,首先需要指定你将要操作的数据库 USE $数据库名称 2. 使用SHOW 命令可以查看数据库和表中的信息 SHOW DATABASES; #列出可用数 ...
- Dubbo-Admin 2.6.0使用
一.下载源码 下载2.6.0的源码 https://github.com/apache/incubator-dubbo/releases/tag/dubbo-2.6.0 二.使用Eclipse打开du ...
- 使用LAP数据集进行年龄训练及估计
一.背景 原本是打算按<DEX Deep EXpectation of apparent age from a single image>进行表面年龄的训练,可由于IMDB-WIKI的数据 ...
- h5需要的浏览器插件
google浏览器插件: web前段助手.vue-tools.草料二维码
- Log4j配置发邮件功能
# 发送日志到指定邮件log4j.appender.mail=org.apache.log4j.net.SMTPAppenderlog4j.appender.mail.Threshold=DEBUGl ...
- Azure CosmosDB (5) 高可用性
<Windows Azure Platform 系列文章目录> Azure Cosmos DB 透明地复制与您的Cosmos帐户关联的所有Azure区域中的数据. Cosmos DB 对数 ...
- 【Leecode】两数相加
学习了链表结构,链表中元素的访问,对象指针的初始化与赋值,链表的创建(多个节点链接起来),进位计算的表达. 100ms /** * Definition for singly-linked list. ...
- mysql数据库目录my.ini的内容
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] #设置3306端口 port = 3306 # 设置mysql的 ...