爬虫—Selenium爬取JD商品信息
一,抓取分析
本次目标是爬取京东商品信息,包括商品的图片,名称,价格,评价人数,店铺名称。抓取入口就是京东的搜索页面,这个链接可以通过直接构造参数访问https://search.jd.com/Search?keyword=iPhone,显示的就是第一页的搜索结果。

页面下方有一个分页导航,包括前7页的链接,下一页的链接和跳转任意页面的链接。

这里的商品显示结果一般都是100页,要获取每一页的内容只需要将页码从1到100遍历即可。所以,我们直接在页面输入框中输入要跳转的页面,点击确定按钮就跳转到对应的页面。
我们在这里不使用直接点击“下一页”,一旦爬取过程中出现异常退出,就无法切换到后续页面了。并且,在爬取过程中也需要记住当前的页码数。当我们利用Selenium加载某一页的商品后,在用相应的解析库解析即可。
二,获取商品列表
我们首先需要构造一个抓取的URL:https://search.jd.com/Search?keyword=iPhone,参数keyword就是要搜索的关键字。只要改变这个参数,就能获取不同商品的列表,这里我们可以将它定义为一个变量。
# _*_ coding=utf-8 _*_ import pymongo
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from urllib.parse import quote # 配置Selenium
browser = webdriver.Chrome(executable_path=r'D:\Google\Chrome\Application\chromedriver')
wait = WebDriverWait(browser, 10)
keyword = 'iPhone' # 配置MongoDB
MONGO_URL = 'localhost'
MONGO_DB = 'JD'
MONGO_COLLECTION = 'goods'
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB] def get_page(page):
"""
获取页面
:param page: 页码
:return:
"""
print('正在爬取第', page, '页')
try:
url = 'https://search.jd.com/Search?keyword=' + quote(keyword)
browser.get(url) if page >= 1:
# 页码搜索框加载成功
search_page = wait.until(
EC.presence_of_element_located(
(By.XPATH, '//div[@id="J_searchWrap"]//div[@id="J_bottomPage"]//span[2]/input'))
)
# 页码确认按钮加载成功
submit = wait.until(
EC.element_to_be_clickable((By.XPATH, '//div[@id="J_searchWrap"]//div[@id="J_bottomPage"]//span[2]/a')))
print('button')
search_page.clear()
search_page.send_keys(page)
submit.click() # 当前页码显示标识加载成功,对比我们传入的page,结果一致就返回True,证明是跳转到了传入的page页面
wait.until(
EC.text_to_be_present_in_element(
(By.XPATH, '//div[@id="J_searchWrap"]//div[@id="J_bottomPage"]/span//a[@class="curr"]'), str(page)) )
# 商品列表加载成功
wait.until(
EC.presence_of_element_located((By.XPATH, '//div[@id="J_searchWrap"]//div[@id="J_goodsList"]/ul//li'))
)
print('Goods show successfully')
get_goods()
except TimeoutException:
get_page(page) def get_goods():
"""
获取商品数据
:return:
"""
items = browser.find_elements_by_xpath('//div[@id="J_searchWrap"]//div[@id="J_goodsList"]/ul//li')
for item in items:
goods = {
'img': item.find_element_by_xpath('//div[@class="p-img"]/a/img').get_attribute('src'),
'price': item.find_element_by_xpath('//div[@class="p-price"]/strong').text,
'commit': item.find_element_by_xpath('//div[@class="p-commit"]/strong').text,
'title': item.find_element_by_xpath('//div[@class="p-name p-name-type-2"]/a').text,
'shop': item.find_element_by_xpath('//div[@class="p-shop"]/span/a').text,
}
print(goods)
save_to_mongo(goods) def save_to_mongo(result):
"""
保存到MongoDB
:param result: 抓取到的结果:单个商品信息
:return:
"""
try:
if db[MONGO_COLLECTION].insert(result):
print('储存到MongoDB成功!')
except Exception:
print('存储到MongoDB失败!') if __name__ == '__main__':
for i in range(1, 10):
get_page(i)
爬虫—Selenium爬取JD商品信息的更多相关文章
- selenium模块使用详解、打码平台使用、xpath使用、使用selenium爬取京东商品信息、scrapy框架介绍与安装
今日内容概要 selenium的使用 打码平台使用 xpath使用 爬取京东商品信息 scrapy 介绍和安装 内容详细 1.selenium模块的使用 # 之前咱们学requests,可以发送htt ...
- 利用selenium爬取京东商品信息存放到mongodb
利用selenium爬取京东商城的商品信息思路: 1.首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构 2.根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归.等待 ...
- 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爬取京东商品
这篇文章,我们将通过 selenium 模拟用户使用浏览器的行为,爬取京东商品信息,还是先放上最终的效果图: 1.网页分析 (1)初步分析 原本博主打算写一个能够爬取所有商品信息的爬虫,可是在分析过程 ...
- selenium+phantomjs爬取京东商品信息
selenium+phantomjs爬取京东商品信息 今天自己实战写了个爬取京东商品信息,和上一篇的思路一样,附上链接:https://www.cnblogs.com/cany/p/10897618. ...
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- Python爬虫-爬取京东商品信息-按给定关键词
目的:按给定关键词爬取京东商品信息,并保存至mongodb. 字段:title.url.store.store_url.item_id.price.comments_count.comments 工具 ...
- Scrapy实战篇(七)之Scrapy配合Selenium爬取京东商城信息(下)
之前我们使用了selenium加Firefox作为下载中间件来实现爬取京东的商品信息.但是在大规模的爬取的时候,Firefox消耗资源比较多,因此我们希望换一种资源消耗更小的方法来爬取相关的信息. 下 ...
随机推荐
- SQL Server vNext CTP 1.2
https://msdn.microsoft.com/en-us/library/mt788653.aspx
- guava缓存设置return null一直报错空指针
guava缓存设置return null一直报错空指针 因为缓存不允许返回为空
- 配置Python 2.7.1外加环境pywin32-216.win32-py2.7
python-2.7.1 安装包 下载地址:http://download.csdn.net/detail/baidu_14854543/7985187 pywin32-216.win32-py2. ...
- redis connetced refused remote
239down vote I've been stuck with the same issue, and the preceding answer did not help me (albeit w ...
- ubuntu安装ftp环境
ubuntu安装ftp环境 安装: apt install vsftpd 启动: service vsftpd start 查看状态: service vsftpd status root登录: vi ...
- android JNI 资料大全
AndroidJNI 通过C++调用JAVA 1. JNIEnv对象 对于本地函数 JNIEXPORT void JNICALL Java_video1_TestNative_sayHel ...
- Robot Framework操作
Robot Framework 介绍 RobotFramework是一款基于python的开源自动化测试框架,遵守Apache License 2.0协议,在此协议下所有人都可以免费开发和使用.因为R ...
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
Visual Studio 2017中使用正则修改部分内容 最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效 ...
- ExtJs中多个form情况下指定某个form使能
采用extjs的时候,如果一个页面存在多个,那么提交之时,究竟是哪个form使能,就要指明.我今天就遇到了这种情况:明明页面已经有提交,为啥没有提交到内容?一查才知道,我的页面是有2个form,我本意 ...
- 2016/05/05 smarty ①分页 ② 查询后分页 ③缓存
samrty 分页 查询后分页 0505fch.php <?php include("init.inc.php"); include("DBDA.php&qu ...