基于selenium爬取京东
爬取iphone
注意:browser对象会发生变化,当对当前网页做任意操作时
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# if __name__ == '__main__': browser = webdriver.Chrome()
browser.get('https://www.jd.com')
# 搜索iphone
_input = browser.find_element_by_id('key')
_input.send_keys('iphone')
_input.send_keys(Keys.ENTER)
time.sleep(5)
# 按销量排序
sales = browser.find_element_by_xpath('//div[@class="f-sort"]/a[2]')
sales.click() has_next = True
while has_next:
# 获取当前的页码
time.sleep(5)
cur_page = browser.find_element_by_xpath('//div[@id="J_bottomPage"]/span[@class="p-skip"]/input').get_attribute('value')
print('------------------------- 当前页码 {} -------------------------'.format(cur_page)) # 加载全部数据,数据随着滚动条的下来而加载
# good_list = browser.find_element_by_id('J_goodsList')
# y = good_list.rect['y'] + good_list.rect['height']
next_page = browser.find_element_by_class_name('pn-next')
y = next_page.location['y']
browser.execute_script('window.scrollTo(0, {})'.format(y))
time.sleep(3)
# 获取当前页面所有商品列表
p_list = browser.find_elements_by_class_name('gl-item')
for p in p_list:
production = {}
sku = p.get_attribute('data-sku')
production['price'] = p.find_element_by_css_selector('strong.J_{}'.format(sku)).text
production['name'] = p.find_element_by_css_selector('div.p-name>a>em').text
production['comment'] = p.find_element_by_id('J_comment_{}'.format(sku)).text
production['shop'] = p.find_element_by_css_selector('div.p-shop>span>a').get_attribute('title')
print(production) # 下一页
cur_next_page = browser.find_element_by_class_name('pn-next')
# 判断是否是最后一页
if 'disabled' in cur_next_page.get_attribute('class'):
has_next = False
else:
cur_next_page.click() browser.quit()
优化
import time
import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# if __name__ == '__main__':
keyword = 'iphone'
if len(sys.argv) > 1:
keyword = sys.argv[1]
browser = webdriver.Chrome()
browser.get('https://www.jd.com')
# 搜索iphone
_input = browser.find_element_by_id('key')
_input.send_keys(keyword)
_input.send_keys(Keys.ENTER)
time.sleep(5)
# 按销量排序
sales = browser.find_element_by_xpath('//div[@class="f-sort"]/a[2]')
sales.click() has_next = True
while has_next:
# 获取当前的页码
time.sleep(5)
cur_page = browser.find_element_by_xpath('//div[@id="J_bottomPage"]/span[@class="p-skip"]/input').get_attribute('value')
print('------------------------- 当前页码 {} -------------------------'.format(cur_page)) # 加载全部数据,数据随着滚动条的下来而加载
# good_list = browser.find_element_by_id('J_goodsList')
# y = good_list.rect['y'] + good_list.rect['height']
next_page = browser.find_element_by_class_name('pn-next')
y = next_page.location['y']
browser.execute_script('window.scrollTo(0, {})'.format(y))
time.sleep(3)
# 获取当前页面所有商品列表
p_list = browser.find_elements_by_class_name('gl-item')
for p in p_list:
production = {}
sku = p.get_attribute('data-sku')
production['price'] = p.find_element_by_css_selector('strong.J_{}'.format(sku)).text
production['name'] = p.find_element_by_css_selector('div.p-name>a>em').text
production['comment'] = p.find_element_by_id('J_comment_{}'.format(sku)).text
production['shop'] = p.find_element_by_css_selector('div.p-shop>span>a').get_attribute('title')
print(production) # 下一页
cur_next_page = browser.find_element_by_class_name('pn-next')
# 判断是否是最后一页
if 'disabled' in cur_next_page.get_attribute('class'):
has_next = False
else:
cur_next_page.click() browser.quit()
通过sys使
python jd.py mac
补充
sys.argv[0] 是脚本的名称
sys.argv[1] 是参数
基于selenium爬取京东的更多相关文章
- selenium模块使用详解、打码平台使用、xpath使用、使用selenium爬取京东商品信息、scrapy框架介绍与安装
今日内容概要 selenium的使用 打码平台使用 xpath使用 爬取京东商品信息 scrapy 介绍和安装 内容详细 1.selenium模块的使用 # 之前咱们学requests,可以发送htt ...
- 利用selenium爬取京东商品信息存放到mongodb
利用selenium爬取京东商城的商品信息思路: 1.首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构 2.根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归.等待 ...
- Scrapy实战篇(八)之Scrapy对接selenium爬取京东商城商品数据
本篇目标:我们以爬取京东商城商品数据为例,展示Scrapy框架对接selenium爬取京东商城商品数据. 背景: 京东商城页面为js动态加载页面,直接使用request请求,无法得到我们想要的商品数据 ...
- 爬虫系列(十三) 用selenium爬取京东商品
这篇文章,我们将通过 selenium 模拟用户使用浏览器的行为,爬取京东商品信息,还是先放上最终的效果图: 1.网页分析 (1)初步分析 原本博主打算写一个能够爬取所有商品信息的爬虫,可是在分析过程 ...
- Scrapy实战篇(七)之Scrapy配合Selenium爬取京东商城信息(下)
之前我们使用了selenium加Firefox作为下载中间件来实现爬取京东的商品信息.但是在大规模的爬取的时候,Firefox消耗资源比较多,因此我们希望换一种资源消耗更小的方法来爬取相关的信息. 下 ...
- 使用Selenium爬取京东电商数据(以手机商品为例)
进入京东(https://www.jd.com)后,我如果搜索特定的手机产品,如oppo find x2,会先出现如下的商品列表页: 如果点击进入其中一个商品会进入到如下图所示的商品详情页,可以看到用 ...
- Scrapy实战篇(六)之Scrapy配合Selenium爬取京东信息(上)
在之前的一篇实战之中,我们已经爬取过京东商城的文胸数据,但是前面的那一篇其实是有一个缺陷的,不知道你看出来没有,下面就来详细的说明和解决这个缺陷. 我们在京东搜索页面输入关键字进行搜索的时候,页面的返 ...
- 爬虫(十七):Scrapy框架(四) 对接selenium爬取京东商品数据
1. Scrapy对接Selenium Scrapy抓取页面的方式和requests库类似,都是直接模拟HTTP请求,而Scrapy也不能抓取JavaScript动态谊染的页面.在前面的博客中抓取Ja ...
- python爬虫——用selenium爬取京东商品信息
1.先附上效果图(我偷懒只爬了4页) 2.京东的网址https://www.jd.com/ 3.我这里是不加载图片,加快爬取速度,也可以用Headless无弹窗模式 options = webdri ...
随机推荐
- P1057 迷宫路径
题目描述 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...
- vue-上传文件
<label for="exampleInputFile">头像</label> <img :src=" imgsrc != '' ? im ...
- 2018-10-22-win10-uwp-自定义控件入门
title author date CreateTime categories win10 uwp 自定义控件入门 lindexi 2018-10-22 09:47:54 +0800 2018-10- ...
- C# 获取进程退出代码
我需要写一个程序,让这个程序知道另一个程序是否正常退出,于是就需要获取这个进程的退出代码 在程序如果需要手动退出,可以设置当前的退出代码 static void Main(string[] args) ...
- 如何用python“优雅的”调用有道翻译?
前言 其实在以前就盯上有道翻译了的,但是由于时间问题一直没有研究(我的骚操作还在后面,记得关注),本文主要讲解如何用python调用有道翻译,讲解这个爬虫与有道翻译的js“斗争”的过程! 当然,本文仅 ...
- 一个简单的Web服务器-支持静态资源请求
目标 实现一个简单的Web服务器,能够根据HTTP请求的URL响应对应的静态资源,如果静态资源不存在则响应404. HttpServer 使用ServerSocket实现的一个服务器,request根 ...
- 【u033】地震逃生
Time Limit: 1 second Memory Limit: 64 MB [问题描述] 汶川地震发生时,四川**中学正在上课,一看地震发生,老师们立刻带领x名学生逃跑,整个学校可以抽象地看成一 ...
- Libra和中国央行数字货币(DCEP)的对比
最近偶然和朋友讨论起Libra,对Libra和央行的数字货币方案很感兴趣.梳理了阅读资料(参考见文末)和自己的思考,发知乎留个记录. Libra 是什么? 无国界货币 + 为全球数十亿人服务的金融基础 ...
- C# 发送电子邮件(smtp)
相关享目托管在github: https://github.com/devgis/CSharpCodes
- c++ list的坑
std::list为空时调用pop_front的访问越界问题 std::list为空时调用pop_back访问越界问题 所以在使用pop_front . pop_back要先判断list是否为空 st ...