这次利用Selenium抓取淘宝并用pyquery解析得到商品的图片、名称、价格、购买人数、店铺名称和店铺所在地信息;
iPhoneXR_淘宝搜索
配置准备
- Selenium控制Chrome浏览器自动化操作(配置chromedriver),其实我们还可以利用Ajax接口来构造链接,但是非常繁琐(需要关注接口参数!),直接使用Selenium来模拟浏览器会省去很多事情;
- pyquery解析(直接pycharm安装就好!)
注意
商品搜索一般最大为100页,我们一页一页遍历即可!
代码
代码已做注释
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
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 time,pandas as pd
# 声明配置好的浏览器对象
browser = webdriver.Chrome()
# 等待反应(需要手动扫码登陆)
wait = WebDriverWait(browser, 20)
text = 'iPhoneXR'
def index_page(page):
'''
获取索引页
:param page:
:return:
'''
print('正在爬取第', page,'页')
try:
# 网址url
url = 'https://s.taobao.com/search?q='+ quote(text)
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():
'''
提取商品数据
:return:
'''
# page_source获取网页源代码
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('.deal-cnt').text(),
'title': item.find('.title').text(),
'shop': item.find('.shop').text(),
'location': item.find('.location').text(),
}
print(product)
#主函数
def main():
'''
遍历每一页
:return:
'''
for i in range(1, 101):
index_page(i)
#关闭浏览器
browser.close()
if __name__ == '__main__':
main()
最终效果
- 展示一页的效果
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/imgextra/i4/11641034/O1CN011zk2bQ1JVc3A4nHmh_!!0-saturn_solar.jpg', 'price': '¥4388.00', 'deal': '361人付款', 'title': '现货Apple/苹果 iPhone XR 苹果xr iphonexr美版国行手机iphonex', 'shop': 'wxjking', 'location': '广东 深圳'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/imgextra/i1/15381762/O1CN01hcmEaH1Ot2O3ho9JW_!!0-saturn_solar.jpg', 'price': '¥4588.00', 'deal': '589人付款', 'title': 'Apple/苹果 iPhone XS Max双卡iphonexr苹果手机', 'shop': '我就爱购数码专营店', 'location': '江苏 南京'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/imgextra/i3/26731697/O1CN01s6IywX1OPGdjQ5nyd_!!2-saturn_solar.png', 'price': '¥5499.00', 'deal': '26人付款', 'title': '月供456起 12期分期免息【送1年延保】Apple/苹果 iPhone XR移动联通电信全网通版 苹果xr iphonexr xs', 'shop': '卓瓦数码专营店', 'location': '上海'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i1/2665494600/O1CN01bmxr3y1jqqPn9fjcY_!!2665494600.jpg', 'price': '¥3650.00', 'deal': '94人付款', 'title': 'Apple/苹果 iPhoneXR美版有锁 iPhonexr有锁日版全网通4g手机分期', 'shop': '海飞手机零售店', 'location': '广东 深圳'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i4/40373372/O1CN01XcTfjY1amQ3L5kE3W_!!40373372.jpg', 'price': '¥3399.00', 'deal': '61人付款', 'title': '新Apple/苹果 iPhone XR iPhonexr苹果xr美版有锁4G手机双卡双待', 'shop': 'changeablegirl1', 'location': '广东 深圳'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i3/245493533/O1CN011by9lsB5SCYnRco_!!245493533.jpg', 'price': '¥4930.00', 'deal': '121人付款', 'title': '天津Apple/苹果 iPhone XR 国行 双卡全网通现货当天发 iPhonexr', 'shop': '晓阳通讯', 'location': '天津'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i2/796968628/O1CN01Vw0xMV2Dbfl3v9jYH_!!0-item_pic.jpg', 'price': '¥4978.00', 'deal': '50人付款', 'title': 'Apple/苹果iPhone XS Max iPhoneXs iPhoneXR 双卡国行苹果XS XSM', 'shop': 'handinhand00', 'location': '四川 成都'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i3/3848293053/O1CN011YQJeCK1MBFFCwT_!!3848293053.jpg', 'price': '¥4938.00', 'deal': '75人付款', 'title': '西安Apple/苹果 iPhone XR 苹果XR 6.1寸iphonexr 国行xr双卡手机', 'shop': '炫和科技', 'location': '陕西 西安'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i4/55285307/O1CN011p4eOUxZZoZKKqJ_!!55285307.jpg', 'price': '¥4920.00', 'deal': '41人付款', 'title': '顺丰现货 Apple/苹果 iPhone XR 苹果XR iphone xr iphonexr 新品', 'shop': 'zjdtpprint', 'location': '浙江 杭州'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i1/898225/O1CN01WYGr2x2Ad69qYgl4s_!!898225.jpg', 'price': '¥4088.00', 'deal': '86人付款', 'title': '【当天发货】Apple/苹果 iPhone XR 苹果XR iphonexr双卡国行港版', 'shop': 'ygsd', 'location': '北京'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i1/1832126825/O1CN01PpZJmS20HtfSiD19f_!!1832126825.jpg', 'price': '¥3498.00', 'deal': '22人付款', 'title': 'Apple/苹果 iPhone XR 苹果XR iphone xr美版iphonexr xsmax手机', 'shop': '鱼得水_168', 'location': '广东 深圳'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i4/2001444944/O1CN01aJkjNi1mOOdbBuIBz_!!2001444944.jpg', 'price': '¥3658.00', 'deal': '66人付款', 'title': 'Apple/苹果 iPhone XR 苹果XR iphonexr 日版美版有锁无锁版手机', 'shop': '神速手机', 'location': '广东 深圳'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i2/2602427769/O1CN01W8mQ1z27GFWmgUrIn_!!2602427769.jpg', 'price': '¥4678.00', 'deal': '15人付款', 'title': 'Apple/苹果 iPhone XR iphonexr手机 苹果XR 国行双卡4G手机8', 'shop': '轩依尚网店', 'location': '广东 深圳'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i1/1579139445/O1CN01ZUi79R2JdrM6DCL6t_!!1579139445.jpg', 'price': '¥3800.00', 'deal': '8人付款', 'title': '【分期免息】Apple/苹果 iPhone XR全网通4G双卡iPhonexr全新手机', 'shop': '丽婵是我', 'location': '广东 广州'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i3/845035944/O1CN018AEjYT1tmOj09qKwu_!!845035944.jpg', 'price': '¥4280.00', 'deal': '14人付款', 'title': 'Apple/苹果 iPhone XR 全网通智能国行4G手机 iPhonexr12期分期', 'shop': '给力小娘子', 'location': '上海'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i2/2784455686/O1CN01UaO7Bj1rsEVlWafxv_!!2784455686.jpg', 'price': '¥3650.00', 'deal': '30人付款', 'title': 'Apple/苹果 iPhone XR手机xs max新iphonexr港版国行美版有锁日版', 'shop': '睿祖电器', 'location': '广东 广州'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i4/475027050/O1CN012ldzOd21wwmEWDBxq_!!475027050.jpg', 'price': '¥4970.00', 'deal': '43人付款', 'title': '现货Apple/苹果 iPhone XR 苹果XR 国行iphone xr 港版iphonexr', 'shop': '860803华晶', 'location': '浙江 杭州'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i2/2695630320/O1CN01vekJAm1EEbFNbWFcv_!!2695630320.jpg', 'price': '¥3500.00', 'deal': '27人付款', 'title': 'Apple/苹果 iPhone XR 苹果xr 6.1寸iphonexr双卡双待iphonex手机', 'shop': '牛点贸易有限公司', 'location': '广东 深圳'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i4/2283672232/O1CN01KuskP61SMIXDn0qL0_!!2283672232.jpg', 'price': '¥4790.44', 'deal': '113人付款', 'title': 'Apple/苹果 iPhone XR苹果 xr iphonexr 双卡双待苹果xr国行手机', 'shop': '雨泽2709', 'location': '广东 深圳'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i4/3000223709/O1CN019JfxbA1dGlVrVAWE1_!!3000223709.jpg', 'price': '¥3880.00', 'deal': '43人付款', 'title': '全新Apple/苹果 iPhone XR国行港版双卡无锁 美版有锁iphonexr', 'shop': '鱼水源企业店', 'location': '广东 深圳'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i3/11628952/O1CN01cyMsDt2G045Np1Bo3_!!11628952.jpg', 'price': '¥4380.00', 'deal': '43人付款', 'title': '现货速发 Apple/苹果 iPhone XR iphonexr苹果XR新款6.1寸手机', 'shop': '壶煮三江雪', 'location': '四川 成都'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i1/436038575/O1CN012DDOigI3fydX2mH_!!436038575.png', 'price': '¥4078.00', 'deal': '34人付款', 'title': '新品Apple/苹果 iPhone XS Max iPhoneXR 苹果iPhone Xs 现货秒发', 'shop': 'wangying15870022201', 'location': '广东 广州'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i1/2661198246/O1CN010BRq5r2AmiTcSiYvj_!!2661198246.jpg', 'price': '¥4789.00', 'deal': '25人付款', 'title': 'Apple/苹果 iPhone XR 苹果iPhonexr 国行全网通双卡双待4G手机', 'shop': '盈利通贸易有限公司', 'location': '广东 深圳'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i3/2617607551/O1CN0125ePA55NtourGTX_!!2617607551.jpg', 'price': '¥3000.00', 'deal': '44人付款', 'title': 'Apple/苹果 iPhone XR 国行港版苹果 iphonexr手机XR韩版美版有锁', 'shop': '杭州拉风数码', 'location': '浙江 杭州'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i2/20865170/O1CN011o3u7tf2NGfefqa_!!20865170.jpg', 'price': '¥4200.00', 'deal': '35人付款', 'title': 'Apple/苹果 iPhone XR国行苹果手机iphoneXR6.1寸全屏重庆实体XR', 'shop': '邂逅水清清', 'location': '重庆'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i4/25483223/O1CN011ZgAzM8xDjYGcYK_!!25483223.jpg', 'price': '¥4938.00', 'deal': '37人付款', 'title': '现货Apple/苹果 iPhone XR 6.1寸 新款 iPhoneXR 行货港版国行', 'shop': '糊涂在线', 'location': '四川 成都'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i3/696120373/O1CN011EcsCXyyKJKB8wW_!!696120373.jpg', 'price': '¥4918.00', 'deal': '29人付款', 'title': '现货Apple/苹果 iPhone XR 6.1英寸国行手机 iPhonexr全网通双卡', 'shop': '古龙圣宝', 'location': '北京'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i4/30768631/O1CN012Dd2uVdbCeVMP3L_!!30768631.jpg', 'price': '¥4630.00', 'deal': '21人付款', 'title': 'Apple/苹果 iPhone XR iphonexr手机 苹果XR 9 双卡 国行原封现货', 'shop': '洋帆数码', 'location': '湖北 武汉'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i3/786406010/O1CN01YTBMCP1uGcqAbvz5N_!!786406010.png', 'price': '¥3999.00', 'deal': '53人付款', 'title': 'Apple/苹果 iPhone XR国行美版韩版日版无锁有锁苹果iPhoneXR', 'shop': '王树超555', 'location': '山东 淄博'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i3/2359568200/O1CN01SytjXB2AReDYHAAJX_!!2359568200.jpg', 'price': '¥4288.00', 'deal': '28人付款', 'title': '国行正品Apple/苹果 iPhone XR iPhone xr 港版港行iphonexr现货', 'shop': '秦彤彤2013', 'location': '北京'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i2/2583126917/O1CN01avTTBe20y24jlGqbp_!!2583126917.jpg', 'price': '¥4050.00', 'deal': '5人付款', 'title': '【飞碟数码】Apple/苹果 iPhone XR 苹果XR手机iphonexr XS MAX', 'shop': '然而我却是李大爷', 'location': '广东 深圳'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i1/721555557/O1CN01Y6xQih1qv9SFRd4dd_!!721555557.jpg', 'price': '¥2998.00', 'deal': '19人付款', 'title': 'Apple/苹果 iPhone XR 苹果xr 双卡xsmax手机现货国行 iPhonexr', 'shop': '云中的燕子8', 'location': '浙江 杭州'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i4/2622831168/O1CN01IolreY1KUz78mAPVp_!!2622831168.jpg', 'price': '¥4780.00', 'deal': '44人付款', 'title': 'Apple/苹果 iPhone XR苹果xr iphonexr xs max国行正品双卡4G', 'shop': '优一贸易', 'location': '广东 深圳'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i4/112417306/O1CN01pzeQXm23qC8g2mheB_!!112417306.jpg', 'price': '¥4960.00', 'deal': '46人付款', 'title': '现货 Apple/苹果 iPhone XR 苹果xr iphonexr 港版国行双卡4G手机', 'shop': '尤里xx', 'location': '江苏 南京'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i3/741010229/O1CN01MgTO881DYvD52mk3G_!!741010229.jpg', 'price': '¥4398.00', 'deal': '37人付款', 'title': '【以旧换新】Apple/苹果 iPhone XR iPhonexr Max 苹果xr港版手机', 'shop': '远航通讯16', 'location': '四川 成都'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i1/406740692/O1CN01Dg6W9Y1GyyaYgsiah_!!406740692.jpg', 'price': '¥4499.00', 'deal': '8人付款', 'title': 'Apple/苹果 iPhone XR 原装正品苹果XR 国行6.1英寸iphonexr双卡', 'shop': 'jiali通讯', 'location': '上海'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i4/1103982598/O1CN01b6azyu1V3vTrpVxms_!!1103982598.jpg', 'price': '¥3720.00', 'deal': '15人付款', 'title': '【光头手机】Apple/苹果 iPhone XR iPhoneXr苹果Xr双卡双待手机9', 'shop': '光头先僧', 'location': '广东 深圳'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i4/45055934/O1CN01FMiDHD1thokXA12wL_!!45055934.jpg', 'price': '¥3298.00', 'deal': '36人付款', 'title': 'Apple/苹果 iPhoneXR 日版美版无有锁全新行货苹果iPhoneXs手机Xr', 'shop': 'yxt23901', 'location': '浙江 杭州'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i2/11247811/O1CN0127ZU8j3Uqon33K6_!!11247811.jpg', 'price': '¥4480.00', 'deal': '38人付款', 'title': 'Apple/苹果 iPhone XR国行原封港版iphonexr双卡手机美版三网无锁', 'shop': 'zxsgtc', 'location': '湖北 武汉'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i3/853982/O1CN011fHnbFQinEv1lo9_!!853982.jpg', 'price': '¥4118.00', 'deal': '11人付款', 'title': '【金冠卖家12期分期当天发货】Apple/苹果 iPhone XR iphonexr', 'shop': '非常手机', 'location': '上海'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i1/32859683/O1CN012LOrZhkTA6tdf5Y_!!32859683.jpg', 'price': '¥4590.00', 'deal': '6人付款', 'title': 'Apple/苹果 iPhone XR iphonexr原封全新未激活6.1寸现货Xr联保xs', 'shop': 'hugo0217', 'location': '江苏 常州'}
{'image': '//g-search1.alicdn.com/img/bao/uploaded/i4/i1/2671825699/O1CN011ryBeIy3gIiZhR7_!!2671825699.jpg', 'price': '¥4958.00', 'deal': '18人付款', 'title': '双卡全网通无线充蓝牙耳机Apple/苹果 iPhone XR苹果手机iPhonexr', 'shop': 'jiute999', 'location': '河南 郑州'}
{'image': '//g-search2.alicdn.com/img/bao/uploaded/i4/i3/2298805202/O1CN011oIYo0RE4Qve8lk_!!2298805202.jpg', 'price': '¥4880.00', 'deal': '5人付款', 'title': 'Apple/苹果 iPhone XR双卡6.1英寸iPhonexr闪送脸部识别A2108换新', 'shop': '时尚美妆小屋2014', 'location': '江苏 无锡'}
{'image': '//g-search3.alicdn.com/img/bao/uploaded/i4/i4/702115986/O1CN01myvC6s1u5dKwdPhJL_!!702115986.jpg', 'price': '¥3768.00', 'deal': '12人付款', 'title': '现货Apple/苹果 iPhone XR港版国行美版新iPhonexr苹果xr有锁无锁', 'shop': '聪明宝贝72', 'location': '广东 深圳'}
正在爬取第 3 页
结语
- 最终是以字典的形式存储的,数据我们已经都获取到了,如果想要存储到excel文件里并且更加美观,可以参照我之前的文章!
- 这次爬取淘宝,东西不多,交个朋友!
- 还是要说一下Selenium,这个工具真不错,不知道的朋友真的可以来试一试!
网友评论