import re
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
from pyquery import PyQuery as pq KEYWORD = '小米手机'
MAX_PAGE = 3 # 浏览器驱动
browser = webdriver.Chrome()
wait = WebDriverWait(browser,10) def get_products():
# 获取网页源代码
html = browser.page_source
# 解析
content = pq(browser.page_source)
# 得到所有选择的内容
items = content('#mainsrp-itemlist .m-itemlist .grid.g-clearfix .item').items()
for item in items:
product = {
'image':item.find('.pic .img').attr('data-src'),
'price':item.find('.price').text().strip(),
'deal':item.find('.deal-cnt').text(),
'title':item.find('.title').text(),
'shop':item.find('.shop').text(),
'location':item.find('.location').text()
} print('--------{}----------\n'.format(product)) def index_page(page):
'''
抓取索引页
:param page:
:return:
'''
print(10*'-','正在抓取第{}页'.format(page),10*'-')
try:
url = 'https://s.taobao.com/search?q={}'.format(KEYWORD)
print(url)
browser.get(url)
# 如果抓取的不是第一页,进行跳页操作
if page > 1:
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)
submit.click()
# 等待页面加载完成(当前高亮页码是page)
wait.until(EC.text_to_be_present_in_element((
By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'
),str(page)))
# 等待所有商品信息加载完成
wait.until(EC.presence_of_element_located((
By.CSS_SELECTOR,'#mainsrp-itemlist .m-itemlist .grid.g-clearfix .item')
))
get_products()
except TimeoutException:
index_page() def main():
# 遍历每一页
for page in range(1,MAX_PAGE+1):
index_page(page)

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

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

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

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

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

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

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

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

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

  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. selenium+pyquery爬取淘宝美食100页(无头静默模式)

    import re from selenium import webdriver from selenium.webdriver.common.by import By from selenium.w ...

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

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

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

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

随机推荐

  1. LC 20 Valid Parentheses

    问题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  2. MySQL8.0哪些新特性你最期待

    1.数据字典全部采用InnoDB引擎存储,支持DDL原子性.crash safe,metadata管理更完善 2.快速在线加新列(腾讯互娱DBA团队贡献) 3.并行redo log,并提升redo l ...

  3. 【Trie】Phone List

    [题目链接]: https://loj.ac/problem/10049 [题意] 问是否存在一组公共前缀.如果存在输出“NO”,否则输出“YES” [题解] 首先建出Trie树来,然后开始记录所有的 ...

  4. 【思维】Kenken Race

    题目描述 There are N squares arranged in a row, numbered 1,2,...,N from left to right. You are given a s ...

  5. linux实现一个定时任务

    设置定时任务删除logs脚本数据 编写脚本   touch cleanLogs.sh #! /bin/sh -name "*.log*" -exec rm -f {} \; 使用r ...

  6. shiro 权限过滤器 -------(1)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBEAAAJRCAIAAACcEbhqAAAgAElEQVR4nO3dv67sVtkHYEefhIKUIC ...

  7. spring cloud EurekaClient 多网卡 ip 配置 和 源码分析(转)

    https://blog.csdn.net/qq_30062125/article/details/83856655 1.前言对于spring cloud,各个服务实例需要注册到Eureka注册中心. ...

  8. JS实现旋转的魔方

    js <script> window.onload = function () { let cube = document.querySelector('.cube') let timer ...

  9. 转载:PHP扩展函数库-文件系统、进程与网络

    PHP的扩展函数库十分庞大,官方的非官方的,在这里只记录一些目前比较常用的扩展,对于这一部分,也只是记录其中一些核心的函数,不是一个全面记录.对于详细的扩展函数说明,需要在使用中参考PHP的用户手册. ...

  10. vue 导航守卫

    1.全局守卫(在所有路由展示前触发)//在main.js中 router.beforeEach((to, from, next) => {      to 即将要进入的到的路由,值为路由    ...