from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re
from pyquery import PyQuery as pq
from config import *
import pymongo client = pymongo.MongoClient(MONGO_URL)
db =client[MONGO_DB] browser = webdriver.PhantomJS(service_args=SERVICE_ARGS)
wait = WebDriverWait(browser, 10)# 等待时长10秒,默认0.5秒询问一次,等待页面加载完成,找到某个条件发生后再继续执行后续代码,如果超过设置时间检测不到则抛出异常
browser.set_window_size(1400,900)
def search():
print("正在搜索")
try:
browser.get('https://www.taobao.com/')
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#q")))#模拟输入框
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#J_TSearchForm > div.search-button > button")))
#模拟搜索按钮
input.send_keys('美食')#添加input
submit.click()#模拟按下搜索按钮
total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.total")))
get_product()
return total.text
except TimeoutException:
return search()
def next_page(page_number):#翻页,把当前页码清除后,直接跳转到想去的页码
print("正在翻页",page_number)
try:
input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.form > input")))
submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit")))
input.clear()
input.send_keys(page_number)
submit.click()
wait.until(EC.text_to_be_present_in_element(
(By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > ul > li.item.active > span"),str(page_number)))
get_product()
except TimeoutException:
next_page(page_number)
def get_product():#获得每页商品内容,pyquery not understand
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-itemlist .items .item")))
html =browser.page_source
# print(html)
doc = pq(html)
items = doc("#mainsrp-itemlist .items .item").items()#注意class名字后空格!
for item in items:
product = {
'image':item.find('.pic .img').attr('src'),
'price':item.find('.price').text(),
'deal':item.find('.deal-cnt').text()[:-3],
'title':item.find('.title').text(),
'shop':item.find('.shop').text(),
'location':item.find('.location').text()#find 查找的是div class的名字 别的标签不可以
}
print(product)
save_to_mongo(product)
def save_to_mongo(result):#将数据存储到mongodb
try:
if db[MONGO_TABLE].insert(result):
print('存储成功->',result)
except Exception:
print('存储失败->',result)
def main():
total = search()
total = int(re.compile('\d+').search(total).group(0))
for i in range(2,20):
next_page(i)
browser.close() if __name__ == '__main__':
main()

MONGO_URL = 'localhost'
MONGO_DB = 'taobao'
MONGO_TABLE = 'product' SERVICE_ARGS = ['--load-images=false', '--disk-cache=true']

config


selenium+phantomjs+pyquery 爬取淘宝商品信息的更多相关文章

  1. selenium+pyquery爬取淘宝商品信息

    import re from selenium import webdriver from selenium.common.exceptions import TimeoutException fro ...

  2. Selenium+Chrome/phantomJS模拟浏览器爬取淘宝商品信息

    #使用selenium+Carome/phantomJS模拟浏览器爬取淘宝商品信息 # 思路: # 第一步:利用selenium驱动浏览器,搜索商品信息,得到商品列表 # 第二步:分析商品页数,驱动浏 ...

  3. 利用Selenium爬取淘宝商品信息

    一.  Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...

  4. python3编写网络爬虫16-使用selenium 爬取淘宝商品信息

    一.使用selenium 模拟浏览器操作爬取淘宝商品信息 之前我们已经成功尝试分析Ajax来抓取相关数据,但是并不是所有页面都可以通过分析Ajax来完成抓取.比如,淘宝,它的整个页面数据确实也是通过A ...

  5. <day003>登录+爬取淘宝商品信息+字典用json存储

    任务1:利用cookie可以免去登录的烦恼(验证码) ''' 只需要有登录后的cookie,就可以绕过验证码 登录后的cookie可以通过Selenium用第三方(微博)进行登录,不需要进行淘宝的滑动 ...

  6. 爬取淘宝商品信息,放到html页面展示

    爬取淘宝商品信息 import pymysql import requests import re def getHTMLText(url): kv = {'cookie':'thw=cn; hng= ...

  7. Python 爬取淘宝商品信息和相应价格

    !只用于学习用途! plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html) :获得商品价格和view_pri ...

  8. 3.使用Selenium模拟浏览器抓取淘宝商品美食信息

    # 使用selenium+phantomJS模拟浏览器爬取淘宝商品信息 # 思路: # 第一步:利用selenium驱动浏览器,搜索商品信息,得到商品列表 # 第二步:分析商品页数,驱动浏览器翻页,并 ...

  9. Python 爬取淘宝商品数据挖掘分析实战

    Python 爬取淘宝商品数据挖掘分析实战 项目内容 本案例选择>> 商品类目:沙发: 数量:共100页  4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 爬取淘宝商品 ...

随机推荐

  1. 20175325 MyCP (课下作业,必做)

    20175325 MyCP (课下作业,必做) 一.目录: 题目 设计思路 运行结果 码云链接 二.题目 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两 ...

  2. Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting

    Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting 这篇文章主要是了解方法. 原始文 ...

  3. apache的.htaccess文件作用和相关配置

    首先.htaccess什么? .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. 当我们使用apache部署一个网站代码准备部署到网上的时候,我们手中的apache的h ...

  4. rabbit基本原理 转

    https://www.cnblogs.com/jun-ma/p/4840869.html

  5. YAML-CPP

    yaml作为一种便捷的文件格式,通过c++进行操作时,可以利用yaml-cpp进行. 一,yaml-cpp的安装 下载源码 git clone https://github.com/jbeder/ya ...

  6. Appium+Python自动化 2 定位元素方式

    1.找到 Android SDK安装路径tools 下面的 uiautomatorviewer.bat,如下截图 2.点击uiautomatorviewer.bat进行启动,左上角一共四个按钮,作用分 ...

  7. vue框架中的日期组件转换为yyy-mm-dd格式

    最近在用vue框架写一个app,这个是用到的日期格式转换,把下面的标准格式转换为字符串连接格式

  8. Git与SVN的区别(面试常问)

    1.Git是分布式的,而SVN不是分布式的 2.Git把内容按元数据方式存储,而SVN是按文件 3.Git没有一个全局版本号,SVN有,目前为止这是SVN相比Git缺少的最大的一个特征 4.Git的内 ...

  9. linux系统下部署DNS反向解析

    DNS服务概述 DNS(Domain Name System)域名系统,能够提供域名与IP地址的解析服务. 反向解析 反向解析是从IP地址到域名的解析过程.主要作用于服务器的身份验证. 部署反向解析 ...

  10. Linux学习---位运算符

    <<.>> ① << 左移  乘以2^n m << n m*(2^n) eg:4: 0 0 1 0 0 8: 0 1 0 0 0 [数据.数字]移位 左 ...