selenium+phantomjs+pyquery 爬取淘宝商品信息
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 爬取淘宝商品信息的更多相关文章
- selenium+pyquery爬取淘宝商品信息
import re from selenium import webdriver from selenium.common.exceptions import TimeoutException fro ...
- Selenium+Chrome/phantomJS模拟浏览器爬取淘宝商品信息
#使用selenium+Carome/phantomJS模拟浏览器爬取淘宝商品信息 # 思路: # 第一步:利用selenium驱动浏览器,搜索商品信息,得到商品列表 # 第二步:分析商品页数,驱动浏 ...
- 利用Selenium爬取淘宝商品信息
一. Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...
- python3编写网络爬虫16-使用selenium 爬取淘宝商品信息
一.使用selenium 模拟浏览器操作爬取淘宝商品信息 之前我们已经成功尝试分析Ajax来抓取相关数据,但是并不是所有页面都可以通过分析Ajax来完成抓取.比如,淘宝,它的整个页面数据确实也是通过A ...
- <day003>登录+爬取淘宝商品信息+字典用json存储
任务1:利用cookie可以免去登录的烦恼(验证码) ''' 只需要有登录后的cookie,就可以绕过验证码 登录后的cookie可以通过Selenium用第三方(微博)进行登录,不需要进行淘宝的滑动 ...
- 爬取淘宝商品信息,放到html页面展示
爬取淘宝商品信息 import pymysql import requests import re def getHTMLText(url): kv = {'cookie':'thw=cn; hng= ...
- Python 爬取淘宝商品信息和相应价格
!只用于学习用途! plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html) :获得商品价格和view_pri ...
- 3.使用Selenium模拟浏览器抓取淘宝商品美食信息
# 使用selenium+phantomJS模拟浏览器爬取淘宝商品信息 # 思路: # 第一步:利用selenium驱动浏览器,搜索商品信息,得到商品列表 # 第二步:分析商品页数,驱动浏览器翻页,并 ...
- Python 爬取淘宝商品数据挖掘分析实战
Python 爬取淘宝商品数据挖掘分析实战 项目内容 本案例选择>> 商品类目:沙发: 数量:共100页 4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 爬取淘宝商品 ...
随机推荐
- Docker容器镜像瘦身的三个小窍门(转)
[转自:http://dockone.io/article/8174] 在构建Docker容器时,我们应尽可能减小镜像的大小.使用共享层的镜像尺寸越小,其传输和部署速度越快. 不过在每个RUN语句都会 ...
- 使用FFmpeg解码并用swscale将YUV转为RGB
#include <stdio.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h&g ...
- leetcode 字谜
242. Valid Anagram Easy 66298FavoriteShare Given two strings s and t , write a function to determine ...
- AUDIOqueue 为什么会播放一段时间就听不到声音
转自简书:非常有用 AudioQueue缓冲区为空时,那么AudioQueueOutputCallback回调不会再调用 这个其实很好理解,AudioQueue的回调本事就是数据播完了才回调的 Aud ...
- java 编程英语单词,语句
记录一下java 编程工作学习中常用的英语汇总 in other words: 换句话说 dangle :悬挂 separated:分开的 distinct:明显的,独特的 actual :实际的 i ...
- win10操作系统上,wireshark抓取https。
今天试了下使用wireshark抓https的包 一.记录如下: 配置一个环境变量SSLKEYLOGFILE为D:\Temp\sslog.log(这个文件需要自己去创建). 去下载一个chrome浏览 ...
- DataTable行分组,并sum求和
两种方式: 第一种,Linq void Main() { var dt=new DataTable(); dt.Columns.Add("medicID"); dt.Columns ...
- JVM总括二-垃圾回收:GC Roots、回收算法、回收器
JVM总括二-垃圾回收:GC Roots.回收算法.回收器 目录:JVM总括:目录 一.判断对象是否存活 为了判断对象是否存活引入GC Roots,如果一个对象与GC Roots没有直接或间接的引用关 ...
- centos7 删除swap
https://www.refmanual.com/2016/01/08/completely-remove-swap-on-ce7/#.W8AaSRMzaRs 删除不干净,启动不起来的情况下.需要从 ...
- laraval migration 新增字段或者修改字段的方法
1.进入项目根目录执行artisan命令生成migration文件,可以指定--table和--path参数,会在对应目录下生成migration文件. php artisan make:migrat ...