爬取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爬取京东的更多相关文章

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

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

  2. 利用selenium爬取京东商品信息存放到mongodb

    利用selenium爬取京东商城的商品信息思路: 1.首先进入京东的搜索页面,分析搜索页面信息可以得到路由结构 2.根据页面信息可以看到京东在搜索页面使用了懒加载,所以为了解决这个问题,使用递归.等待 ...

  3. Scrapy实战篇(八)之Scrapy对接selenium爬取京东商城商品数据

    本篇目标:我们以爬取京东商城商品数据为例,展示Scrapy框架对接selenium爬取京东商城商品数据. 背景: 京东商城页面为js动态加载页面,直接使用request请求,无法得到我们想要的商品数据 ...

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

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

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

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

  6. 使用Selenium爬取京东电商数据(以手机商品为例)

    进入京东(https://www.jd.com)后,我如果搜索特定的手机产品,如oppo find x2,会先出现如下的商品列表页: 如果点击进入其中一个商品会进入到如下图所示的商品详情页,可以看到用 ...

  7. Scrapy实战篇(六)之Scrapy配合Selenium爬取京东信息(上)

    在之前的一篇实战之中,我们已经爬取过京东商城的文胸数据,但是前面的那一篇其实是有一个缺陷的,不知道你看出来没有,下面就来详细的说明和解决这个缺陷. 我们在京东搜索页面输入关键字进行搜索的时候,页面的返 ...

  8. 爬虫(十七):Scrapy框架(四) 对接selenium爬取京东商品数据

    1. Scrapy对接Selenium Scrapy抓取页面的方式和requests库类似,都是直接模拟HTTP请求,而Scrapy也不能抓取JavaScript动态谊染的页面.在前面的博客中抓取Ja ...

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

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

随机推荐

  1. linux 处理器特定的寄存器

    如果你需要测量非常短时间间隔, 或者你需要非常高精度, 你可以借助平台依赖的资源, 一个要精度不要移植性的选择. 在现代处理器中, 对于经验性能数字的迫切需求被大部分 CPU 设计中内在的指令定时不 ...

  2. 28款GitHub最流行的开源机器学习项目,推荐GitHub上10 个开源深度学习框架

    20 个顶尖的 Python 机器学习开源项目 机器学习 2015-06-08 22:44:30 发布 您的评价: 0.0 收藏 1收藏 我们在Github上的贡献者和提交者之中检查了用Python语 ...

  3. H3C配置历史命令缓冲大小--接口视图(console为准)

    [wang]user-interface console 0 [wang-ui-console0]history-command max-size 30    //配置缓冲区大小 [wang-ui-c ...

  4. Python的驻留机制(仅对数字,字母,下划线有效)

    Python的驻留机制及为在同一运行空间内,当两变量的值相同,则地址也相同. 举例: a = 'abc' b = 'abc' print(id(a)) print(id(b)) 以上示例为驻留机制有效 ...

  5. windows编译caffe2遇到的问题

    首先介绍下window编译caffe2整体流程: 说明:如果不需要python支持只需3.4即可,而且编译亦不会出现问题. 1. 安装python2.7,. 我使用的是anaconda python2 ...

  6. TOJ5705动态序列操作(STL or treap)

    传送门:动态序列操作 在一个动态变化的序列中,完成以下基本操作: (1)插入一个整数 (2)删除一个整数 (3)查找序列中最大的数 (4)查找序列中最小的数 (5)求x的前驱(前驱定义为不大于x的序列 ...

  7. CodeForces 1213F (强联通分量分解+拓扑排序)

    传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...

  8. Trendalyzer is an information visualization software

    Trendalyzer is an information visualization software for animation of statistics that was initially ...

  9. 小心Powershell的位数

    我们都知道64位的 Windows 中有两个Powershell,32位的 Windows Powershell(x86)和64位的 Windows Powershell.(当然,32位的Window ...

  10. SpringBoot源码学习系列之启动原理简介

    本博客通过debug方式简单跟一下Springboot application启动的源码,Springboot的启动源码是比较复杂的,本博客只是简单梳理一下源码,浅析其原理 为了方便跟源码,先找个Ap ...