利用selenium以及pyquery,爬取当当网图书信息,并且将数据存入文件以及MongoDB数据库中。

配置文件:

key="python"
MONGO_URL='localhost'
MONGO_DB='dangdang'
MONGO_TABLE='book'

dangdang.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from config import *
from selenium.common.exceptions import TimeoutException
from pyquery import PyQuery as pq
import json
import pymongo driver = webdriver.Chrome() #driver=webdriver.PhantomJS()无界面浏览器
client=pymongo.MongoClient(MONGO_URL)
db=client[MONGO_DB] def search():
"""
获取关键字,并且获取一共有多少页码
:return:
"""
try:
driver.get("http://book.dangdang.com")
input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#key_S"))
)
button=WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#form_search_new > input.button"))
)
input.send_keys(key)
button.click()
html=driver.page_source
doc=pq(html)
li=doc('div.paging > ul > li:nth-last-child(3)')
total_num=li.text()
return total_num
except TimeoutException:
return search() def get_one_page():
"""
获取每一页的信息
:return:
"""
html=driver.page_source
doc=pq(html)
items=doc('#search_nature_rg ul li').items()
for item in items:
yield {
'title':item.find('a').attr('title'),
'src':item.find('a').attr('href'),
'img':item.find('a img').attr('data-original'),
'price':item.find('.search_pre_price').text(),
'discount':item.find('.search_discount').text()
} def save_to_file(result):
"""
将字典对象存入到文件中
:param result:
:return:
"""
with open('dangdang.txt','a',encoding='utf-8') as f:
f.write(json.dumps(result,ensure_ascii=False)+'\n')
f.close() def save_to_mongo(result):
"""
将字典对象存入到数据库
:param result:
:return:
"""
try:
if db[MONGO_TABLE].insert(result):
print('存储成功',result)
except Exception:
print('存储失败') def next_page(num):
"""
获取下一页
:param num:
:return:
"""
try: input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#t__cp"))
) button=WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#click_get_page"))
)
input.clear()
input.send_keys(num)
button.click() except TimeoutException:
return next_page(num) def main():
total_num=int(search())
for i in range(2,total_num+1):
next_page(i)
results=get_one_page()
for result in results:
save_to_file(result)
save_to_mongo(result) if __name__ == "__main__":
main()

存入到文件中的数据:

{"img": null, "src": "http://product.dangdang.com/25228733.html", "price": "¥109.00", "title": " Python数据科学手册", "discount": "定价: (9.6折)"}
{"img": "http://img3m2.ddimg.cn/63/7/25173882-1_b_3.jpg", "src": "http://product.dangdang.com/25173882.html", "price": "¥69.00", "title": " Python游戏编程快速上手 第4版", "discount": "定价: (9.6折)"}
{"img": "http://img3m7.ddimg.cn/75/5/25232007-1_b_3.jpg", "src": "http://product.dangdang.com/25232007.html", "price": "¥79.00", "title": " Python机器学习基础教程", "discount": "定价: (9.6折)"}
{"img": "http://img3m5.ddimg.cn/8/8/25307675-1_b_2.jpg", "src": "http://product.dangdang.com/25307675.html", "price": "¥59.00", "title": " 深度学习入门 基于Python的理论与实现", "discount": "定价: (9.6折)"}
{"img": "http://img3m6.ddimg.cn/10/10/25098886-1_b_2.jpg", "src": "http://product.dangdang.com/25098886.html", "price": "¥79.00", "title": " Python高性能编程", "discount": "定价: (9.6折)"}
{"img": "http://img3m7.ddimg.cn/32/32/27857147-1_b_1.jpg", "src": "http://product.dangdang.com/27857147.html", "price": "¥69.00", "title": " Python深度学习实战:基于TensorFlow和Keras的聊天机器人以及人脸、物体和语音识别", "discount": "定价: (9.6折)"}
{"img": "http://img3m3.ddimg.cn/85/16/25060153-1_b_2.jpg", "src": "http://product.dangdang.com/25060153.html", "price": "¥69.00", "title": " Python极客项目编程", "discount": "定价: (9.6折)"}
{"img": "http://img3m7.ddimg.cn/96/32/26475567-1_b_2.jpg", "src": "http://product.dangdang.com/26475567.html", "price": "¥69.00", "title": " 小小的Python编程故事", "discount": "定价: (9.6折)"}
{"img": "http://img3m6.ddimg.cn/32/4/25339676-1_b_2.jpg", "src": "http://product.dangdang.com/25339676.html", "price": "¥59.00", "title": " Python数据可视化之matplotlib实践", "discount": "定价: (9.6折)"}
{"img": "http://img3m1.ddimg.cn/9/0/25107201-1_b_3.jpg", "src": "http://product.dangdang.com/25107201.html", "price": "¥69.00", "title": " Python与机器学习实战:决策树、集成学习、支持向量机与神经网络算法详解及编程实现", "discount": "定价: (9.6折)"}
{"img": "http://img3m7.ddimg.cn/35/32/25240877-1_b_2.jpg", "src": "http://product.dangdang.com/25240877.html", "price": "¥69.00", "title": " Python数据分析从入门到精通", "discount": "定价: (9.6折)"}
{"img": "http://img3m2.ddimg.cn/15/23/27866832-1_b_3.jpg", "src": "http://product.dangdang.com/27866832.html", "price": "¥69.00", "title": " Python自动化开发实战", "discount": "定价: (8.8折)"}
{"img": "http://img3m6.ddimg.cn/78/36/26485746-1_b_2.jpg", "src": "http://product.dangdang.com/26485746.html", "price": "¥79.00", "title": " Python量化交易实战", "discount": "定价: (8.8折)"}
{"img": "http://img3m5.ddimg.cn/82/2/25218055-1_b_3.jpg", "src": "http://product.dangdang.com/25218055.html", "price": "¥89.00", "title": " Python 3学习笔记(上卷)", "discount": "定价: (9.6折)"}
{"img": "http://img3m4.ddimg.cn/31/7/25219984-1_b_1.jpg", "src": "http://product.dangdang.com/25219984.html", "price": "¥69.00", "title": " Python贝叶斯分析", "discount": "定价: (9.6折)"}
{"img": "http://img3m5.ddimg.cn/43/0/23617285-1_b_1.jpg", "src": "http://product.dangdang.com/23617285.html", "price": "¥38.00", "title": " Python袖珍指南(第五版)", "discount": "定价: (6.9折)"}
{"img": "http://img3m9.ddimg.cn/76/20/24104299-1_b_12.jpg", "src": "http://product.dangdang.com/24104299.html", "price": "¥49.50", "title": " 零基础入门学习Python", "discount": "定价: (8.81折)"}
{"img": "http://img3m7.ddimg.cn/8/12/26913257-1_b_2.jpg", "src": "http://product.dangdang.com/26913257.html", "price": "¥139.00", "title": " Python经典实例", "discount": "定价: (9.6折)"}
{"img": "http://img3m8.ddimg.cn/57/19/25113288-1_b_2.jpg", "src": "http://product.dangdang.com/25113288.html", "price": "¥39.00", "title": " Python设计模式 第2版", "discount": "定价: (9.59折)"}
{"img": "http://img3m8.ddimg.cn/32/34/25253348-1_b_3.jpg", "src": "http://product.dangdang.com/25253348.html", "price": "¥49.00", "title": " Python 3爬虫、数据清洗与可视化实战", "discount": "定价: (9.6折)"}
{"img": "http://img3m8.ddimg.cn/12/34/27859998-1_b_3.jpg", "src": "http://product.dangdang.com/27859998.html", "price": "¥88.00", "title": " Python程序设计", "discount": "定价: (9.61折)"}
{"img": "http://img3m9.ddimg.cn/15/19/24220959-1_b_3.jpg", "src": "http://product.dangdang.com/24220959.html", "price": "¥59.00", "title": " 跟老齐学Python:轻松入门", "discount": "定价: (9.6折)"}
{"img": "http://img3m0.ddimg.cn/39/27/25216230-1_b_6.jpg", "src": "http://product.dangdang.com/25216230.html", "price": "¥128.00", "title": " Head First Python(第二版)", "discount": "定价: (6.9折)"}

存入到MongoDB中:

selenium实战演练的更多相关文章

  1. selenium界面元素定位

    一.        Selenium界面元素定位 本文元素定位以das2为例 #导入包 from selenium import  webdriver #打开火狐驱动 driver=webdriver ...

  2. 技术分享 | Selenium 测试用例编写

    编写Selenium测试用例就是模拟用户在浏览器上的一系列操作,通过脚本来完成自动化测试. 编写测试用例的优势: 开源,免费. 支持多种浏览器 IE,Firefox,Chrome,Safari. 支持 ...

  3. Python爬虫小白入门(四)PhatomJS+Selenium第一篇

    一.前言 在上一篇博文中,我们的爬虫面临着一个问题,在爬取Unsplash网站的时候,由于网站是下拉刷新,并没有分页.所以不能够通过页码获取页面的url来分别发送网络请求.我也尝试了其他方式,比如下拉 ...

  4. Selenium的PO模式(Page Object Model)[python版]

     Page Object Model 简称POM  普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...

  5. selenium元素定位篇

    Selenium webdriver是完全模拟用户在对浏览器进行操作,所有用户都是在页面进行的单击.双击.输入.滚动等操作,而webdriver也是一样,所以需要我们指定元素让webdriver进行单 ...

  6. selenium自动化基础知识

    什么是自动化测试? 自动化测试分为:功能自动化和性能自动化 功能自动化即使用计算机通过编码的方式来替代手工测试,完成一些重复性比较高的测试,解放测试人员的测试压力.同时,如果系统有不份模块更改后,只要 ...

  7. OCP考点实战演练01-备份恢复篇

    本系列宗旨:真正掌握OCP考试中所考察的技能,坚决不做Paper OCP! 实验环境:RHEL 6.4 + Oracle 11.2.0.4 OCP考点实战演练01-备份恢复篇 1.数据库开启归档 2. ...

  8. OCP考点实战演练02-日常维护篇

    本系列宗旨:真正掌握OCP考试中所考察的技能,坚决不做Paper OCP! 实验环境:RHEL 6.4 + Oracle 11.2.0.4 OCP考点实战演练02-日常维护篇 1.数据库体系结构和AS ...

  9. 幼儿园的 selenium

    from selenium import webdriver     *固定开头     b=webdriver.Firefox()              *打开火狐浏览器    browser. ...

随机推荐

  1. 洛谷P4116 Qtree3(树剖+线段树)

    传送门 LCT秒天秒地 树剖比较裸的题了 用线段树记录一下区间的最左边的黑点的编号(因为同一条链上肯定是最左边的深度最小,到根节点距离最近) 然后记得树剖的时候肯定是越后面的答案越优,因为深度越浅 / ...

  2. 【插件开发】—— 13 GEF双击模型事件

    前文回顾: 1 插件学习篇 2 简单的建立插件工程以及模型文件分析 3 利用扩展点,开发透视图 4 SWT编程须知 5 SWT简单控件的使用与布局搭配 6 SWT复杂空间与布局搭配 7 SWT布局详解 ...

  3. 面试杂谈:面试程序员时都应该考察些什么?<转>

    一般来说,一线成熟企业技术岗位的典型招聘流程分为以下几个步骤: 初筛:一般由直接领导的技术经理或HR进行,重点考察教育和工作经历 一面:一般由可能直接与之共事的工程师进行,重点考察基础和工作能力 二面 ...

  4. 数论 HDOJ 5407 CRB and Candies

    题目传送门 题意:求LCM (C(N,0),C(N,1),...,C(N,N)),LCM是最小公倍数的意思,C函数是组合数. 分析:先上出题人的解题报告 好吧,数论一点都不懂,只明白f (n + 1) ...

  5. Tenegrad评价函数 分类: 图像处理 Opencv 2014-11-12 20:46 488人阅读 评论(0) 收藏

    Tenegrad函数式一种常用的图像清晰度评价函数,是一种基于梯度的函数. 在图像处理中,一般认为对焦好的图像具有更尖锐的边缘,故具有更大的梯度函数值. Tenegrad函数使用Sobel算子提取水平 ...

  6. 【LeetCode】树的遍历

    非递归中序遍历: 思路:注释 vector<int> inorderTraversal(TreeNode* root) { vector<int>ret; if(root == ...

  7. 把sed当作命令解释器使用

    [root@sishen ~]# vim script.sed #!/bin/sed -f #交换第一列和第二列 s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g ...

  8. ambari-server启动报错500 status code received on GET method for API:/api/v1/stacks/HDP/versions/2.4/recommendations Error message : Server Error解决办法(图文详解)

    问题详情 来源是,我在Ambari集群里,安装Hue. 给Ambari集群里安装可视化分析利器工具Hue步骤(图文详解 所遇到的这个问题. 然后,去ambari-server的log日志,查看,如下 ...

  9. Java 线程 —— Wait (等待)和 Notify(唤醒)

    Wait (等待)和 Notify(唤醒) 这里讲了一个Wait (等待)和 Notfity(唤醒),下面这个实例(工厂,商店,消费者) 额,然后,你就知道了,需要写三个类:工厂类,Shop类,消费者 ...

  10. 学习笔记 第十三章 使用CSS3新布局

    第13章   使用CSS3新布局 [学习重点] 设计多列布局 设计弹性盒布局样式 使用CSS3布局技术设计适用移动需求的网页 13.1  多列布局 CSS3使用columns属性定义多列布局,用法如下 ...