Python爬取某宝商品数据案例:100页的价格、购买人数等数据!

前言

随着互联网时代的到来,人们更加倾向于互联网购物,某宝又是电商行业的巨头,在某宝平台中有很多商家数据,今天带大家使用python+selenium工具获取这些公开的。

本篇文章适合Python零基础、对爬虫数据采集感兴趣的同学!

环境介绍:

python 3.6pycharmseleniumtime

selenium简介

自动化测试工具,驱动浏览器帮助我们获取到渲染之后的数据

模仿人的行为操作浏览器(用户行为加上代码逻辑的结合)

安装模块

pip install selenium</pre>

# 步骤

##### 安装Webdriver

打开Google浏览器,点击进入设置界面
![](https://upload-images.jianshu.io/upload_images/22699699-7efee1de67cb8173?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

进入界面后点击关于Chrome,然后找到你Google浏览器的版本,安装Webdriver要对应浏览器的版本
![](https://upload-images.jianshu.io/upload_images/22699699-47a8870282b8327e?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/22699699-23de07bd81192b58?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

##### 导入模块


虫 import constans

##### 入口测试代码

 

def search_product(key): “”“模拟搜索商品,登陆账户,获取最大页数”"" driver.get(‘http://www.taobao.com’)driver.find_element_by_id(‘q’).send_keys(key) # 输入框输入商品关键字driver.find_element_by_class_name(‘btn-search’).click() # 点击搜索按钮driver.implicitly_wait(10) # 隐式等待driver.maximize_window() # 最大化浏览器

def main()search_product(keyword) if name == ‘main’:keyword = input(‘请输入你要查询的商品数据:’)driver = webdriver.Chrome()main()



##### 解决登陆和解决滑块验证

 

driver.find_element_by_xpath('//*[@id="fm-login-id"]').send_keys(constans.USERNAME) # 找到用户名输入账号 time.sleep(1) driver.find_element_by_xpath('//*[@id="fm-login-password"]').send_keys(constans.PASSWORD) # 找到密码框输入密码 time.sleep(2) login = driver.find_element_by_xpath("//span[contains(@class, 'btn_slide')]") # 找到滑动验证码滑块 手写 action = ActionChains(driver) # 创建动作连对象 action.click_and_hold(on_element=login) # 点击滑块维持动作 action.move_by_offset(xoffset=258, yoffset=0) # 设置动作链坐标长度 action.pause(0.5).release().perform() # 设置动作链执行时间 释放鼠标 执行动作链 driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click() # 找到登陆按钮点击 driver.implicitly_wait(10) # 隐式等待

def get_product(): for div in divs:info = div.find_element_by_xpath(’.//div[@class=“row row-2 title”]/a’).text # 商品名称price = div.find_element_by_xpath(’.//strong’).text + ‘元’ # 商品价格deal = div.find_element_by_xpath(’.//div[@class=“deal-cnt”]’).text # 付款人数name = div.find_element_by_xpath(’.//div[@class=“shop”]/a’).text # 店铺名称print(info, price, deal, name, sep=’|’) # 分隔符with open(‘data2.csv’, ‘a’, newline=’’) as csvfile: # newline=’’ 指定一行一行写入csvwriter = csv.writer(csvfile, delimiter=’,’) # delimiter=’,’ csv数据的分隔符csvwriter.writerow([info, price, deal, name])```

运行代码,

这样就可以获取第一页的数据了

运行代码,效果如下图:

Python爬取某宝商品数据案例:100页的价格、购买人数等数据!

解析页码

<pre style="margin: 0px; padding: 0px; word-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"> page = driver.find_element_by_xpath('//*[@id="mainsrp-pager"]/div/div/div/div[1]').text  # 找到页码标签
    page = re.findall('(\d+)', page)[0] # print('商品页数:', page)
    return int(page) def main(): """程序的入口"""
    print('正在爬取第1页的数据')
    page = search_product(keyword)
    get_product()

    page_num = 1
    while page_num != page: print('*' * 100) print('正在爬取第{}页的数据'.format(page_num + 1)) print('*' * 100)
        driver.get('https://s.taobao.com/search?q={}&s={}'.format(keyword, 44 * page_num))  # 拼接产品url地址
        driver.implicitly_wait(10)  # 浏览器等待方法
 get_product()
        page_num += 1 driver.quit()


最后运行代码,就可以爬取100页的数据了

Python爬取某宝商品数据案例:100页的价格、购买人数等数据!

为解决初学者学习上的困难,专门建立的Python学习扣qun:784758214,从0基础的python脚本到web开发、爬虫、django、数据挖掘数据分析等,0基础到项目实战的资料都有整理。送给每一位python的小伙伴!每晚分享一些学习的方法和需要注意的小细节,学习路线规划,利用编程赚外快。

源码获取加群:1136192749