python3编写网络爬虫16-使用selenium 爬取淘宝商品信息
一、使用selenium 模拟浏览器操作爬取淘宝商品信息
之前我们已经成功尝试分析Ajax来抓取相关数据,但是并不是所有页面都可以通过分析Ajax来完成抓取。
比如,淘宝,它的整个页面数据确实也是通过Ajax获取的,但是这些Ajax接口参数比较复杂,可能会包含加密密钥等,
所以如果想自己构造Ajax参数,还是比较困难的。
对于这种页面,最方便快捷的抓取方法就是通过Selenium
目标:利用Selenium抓取淘宝商品并用pyquery解析得到商品的图片、名称、价格、购买人数、店铺名称和店铺所在地信息
完整代码
#-*-coding:utf-8-*- #抓取淘宝商品信息 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 from pyquery import PyQuery as pq
import pymongo
import time browser = webdriver.Chrome()
wait = WebDriverWait(browser,15)
KEYWORD = 'iPad' #抓取索引页 def index_page(page):
print('正在爬取第',page,'页') try:
url = 'https://s.taobao.com/search?q='+ quote(KEYWORD)
browser.get(url) if page > 1:
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager div.form > input')))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager div.form > span.btn.J_Submit')))
input.clear()
input.send_keys(page)
submit.click()
wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager li.item.active > span'),str(page)))
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'.m-itemlist .items .item')))
get_products()
except TimeoutException:
index_page(page) #解析商品列表 def get_products():
html = browser.page_source
doc = pq(html)
items = doc('#mainsrp-itemlist .items .item').items() for item in items:
product = {
'image' : item.find('.pic .img').attr('data-src'),
'price' : item.find('.price').text(),
'deal' : item.find('.dral-cnt').text(),
'title' : item.find('.title').text(),
'shop' : item.find('.shop').text(),
'location' : item.find('.localtion').text()
}
print(product)
save_to_mongo(product) #保存到MongoDB MONGO_URL = 'localhost'
MONGO_DB = 'taobao'
MONGO_COLLECTION = 'products'
client = pymongo.MongoClient(host=MONGO_URL,port=27017)
db = client[MONGO_DB]
def save_to_mongo(result):
try:
if db[MONGO_COLLECTION.insert(result)]:
print('存储成功')
except Exception:
print('存储失败') #main函数 MAX_PAGE = 5 def main():
url = 'https://s.taobao.com/search?q=' + quote(KEYWORD)
browser.get(url)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#J_QRCodeImg')))
time.sleep(10)
for i in range(1,MAX_PAGE+1):
index_page(i) main()
python3编写网络爬虫16-使用selenium 爬取淘宝商品信息的更多相关文章
- 利用Selenium爬取淘宝商品信息
一. Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...
- Selenium+Chrome/phantomJS模拟浏览器爬取淘宝商品信息
#使用selenium+Carome/phantomJS模拟浏览器爬取淘宝商品信息 # 思路: # 第一步:利用selenium驱动浏览器,搜索商品信息,得到商品列表 # 第二步:分析商品页数,驱动浏 ...
- <day003>登录+爬取淘宝商品信息+字典用json存储
任务1:利用cookie可以免去登录的烦恼(验证码) ''' 只需要有登录后的cookie,就可以绕过验证码 登录后的cookie可以通过Selenium用第三方(微博)进行登录,不需要进行淘宝的滑动 ...
- 爬取淘宝商品信息,放到html页面展示
爬取淘宝商品信息 import pymysql import requests import re def getHTMLText(url): kv = {'cookie':'thw=cn; hng= ...
- 使用Selenium爬取淘宝商品
import pymongo from selenium import webdriver from selenium.common.exceptions import TimeoutExceptio ...
- Python网络爬虫(6)--爬取淘宝模特图片
经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...
- Selenium爬取淘宝商品概要入mongodb
准备: 1.安装Selenium:终端输入 pip install selenium 2.安装下载Chromedriver:解压后放在…\Google\Chrome\Application\:如果是M ...
- selenium+pyquery爬取淘宝商品信息
import re from selenium import webdriver from selenium.common.exceptions import TimeoutException fro ...
- 使用Pyquery+selenium抓取淘宝商品信息
配置文件,配置好数据库名称,表名称,要搜索的产品类目,要爬取的页数 MONGO_URL = 'localhost' MONGO_DB = 'taobao' MONGO_TABLE = 'phone' ...
随机推荐
- ____利用C#特性Attribute完成对sql语句的拼接
//定义 特性类: public class MyAttribute : Attribute//自定义注解类判断是否是主键 { public bool PrimaryKey = false; publ ...
- asp.net-常用服务器控件-20180329
常用服务器控件 1.文本类型控件 Label控件 TextBox控件 2.按钮类型控件 Button控件 ImageButton控件 3.选择类型控件 CheckBox控件 RadioButton控件 ...
- Hibernate入门(五)---------事务管理
在Hibernate中,可以通过代码来操作管理事务,如通过“Transaction tx = session.beginTransaction()”,开启一个事务,持久化操作后,通过"tx. ...
- C#两个时间相减
原文地址:http://www.jb51.net/article/60177.htm using System; using System.Collections.Generic; using Sys ...
- es6 语法 (Decorator)
修饰器是一个函数,用来修改类的行为(注意:1.函数 2.修改行为 3.对类进行操作) { //修饰器函数定义 target:类本身,name名称,descriptor描述 let readonly ...
- agc002E - Candy Piles(博弈论)
题意 题目链接 Sol Orz SovitPower #include<bits/stdc++.h> #define Pair pair<int, double> #defin ...
- 2018-08-06 在Office的VBA代码里中文命名
在Excel处理数据时, 顺便试了一下VBA代码编辑器里输入中文, 结果显示为乱码. 查了一下发现VBA本身支持Unicode, 但需要设置系统配置使编辑器能够正常显示, 即设置简体中文为Curren ...
- 开源项目商业模式分析(2) - 持续维护的重要性 - Selenium和WatiN
该系列第一篇发布后收到不少反馈,包括: 第一篇里说的MonicaHQ不一定盈利 没错,但是问题在于绝大多数开源项目商业数据并没有公开,从而无法判断其具体是否盈利.难得MonicaHQ是公开的,所以才用 ...
- Python 基于Python从mysql表读取千万数据实践
基于Python 从mysql表读取千万数据实践 by:授客 QQ:1033553122 场景: 有以下两个表,两者都有一个表字段,名为waybill_no,我们需要从tl_waybill_b ...
- vue 构建项目 文件引入
1.vue引用依赖文件. 举例:axios 先安装 axios.如果直接安装 vue-axios 会报错 npm install axios npm install --save axios vue ...