双十一手剁完了吗?教你用Python再剁一遍(Python模拟登录,采集淘宝商品数据)
2023-03-14 22:39:27 时间
前言
11月4日,中国消费者协会在官网发布消费提示,提醒消费者“双十一”购物六点注意事项。主要内容就是对于双十一的“低价”不可迷信,提防商家套路。那么对于我们要怎么样才能选择真正的底价好货呢?
今天带大家使用python+selenium工具获取这些公开的商家数据,可以采集商品的价格和评价做对比
环境介绍
- python 3.8
- pycharm
- selenium
- csv
- time
- random
安装所需的第三方模块
from selenium import webdriver
import time # 时间模块, 可以用于程序的延迟
import random # 随机数模块
from constants import TAO_USERNAME1, TAO_PASSWORD1
import csv # 数据保存的模块
创建一个浏览器
driver = webdriver.Chrome()
执行自动化浏览器的操作
driver.get('https://www.taobao.com/')
driver.implicitly_wait(10) # 设置浏览器的等待,加载数据
driver.maximize_window() # 最大化浏览器
搜索功能
首先,打开开发者工具;然后选择用左上角的工具选中搜索框,然后会帮我们定位到当前选中元素的标签;最后,右键,选择Copy,再选择Xpath语法
def search_product(keyword):
driver.find_element_by_xpath('//*[@id="q"]').send_keys(keyword)
time.sleep(random.randint(1, 3)) # 尽量避免人机检测 随机延迟
driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()
time.sleep(random.randint(1, 3)) # 尽量避免人机检测 随机延迟
word = input('请输入你要搜索商品的关键字:')
# 调用商品搜索的函数
search_product(word)
登录界面
用上面相同的方法,找到所需元素
driver.find_element_by_xpath('//*[@id="f-login-id"]').send_keys(TAO_USERNAME1)
time.sleep(random.randint(1, 3)) # 尽量避免人机检测 随机延迟
driver.find_element_by_xpath('//*[@id="f-login-password"]').send_keys(TAO_PASSWORD1)
time.sleep(random.randint(1, 3)) # 尽量避免人机检测 随机延迟
driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
time.sleep(random.randint(1, 3)) # 尽量避免人机检测 随机延迟
selenium操作的浏览器被识别了, 无法登录
修改浏览器的部分属性, 绕过检测
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
{"source": """Object.defineProperty(navigator, 'webdriver', {get: () => false})"""})
解析商品数据
def parse_data():
divs = driver.find_elements_by_xpath('//div[@class="grid g-clearfx"]/div/div') # 所有的div标签
for div in divs:
try:
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/span[2]').text
location = div.find_element_by_xpath('.//div[@class="location"]').text
detail_url = div.find_element_by_xpath('.//div[@class="pic"]/a').get_attribute('href')
print(info, price, deal, name, location, detail_url)
保存
with open('某宝.csv', mode='a', encoding='utf-8', newline='') as f:
csv_write = csv.writer(f)
csv_write.writerow([info, price, deal, name, location, detail_url])
翻页爬取
找到页面的规律,为一个等差数列,而第一页为0
for page in range(100): # 012
print(f'
==================正在抓取第{page + 1}页数据====================')
url = f'https://s.taobao.com/search?q=%E5%B7%B4%E9%BB%8E%E4%B8%96%E5%AE%B6&s={page * 44}'
# 解析商品数据
parse_data()
time.sleep(random.randint(1, 3)) # 尽量避免人机检测 随机延迟
最后运行代码,得到结果
`
相关文章
- Jmh基准测试,看我怎么用它来测试Mongodb的数据加载性能
- 6月份Github上热门Python项目排行
- 如何运用Python绘制NBA投篮图表
- TIOBE 7 月编程语言排行榜:C、Java 和 Python 争夺第一名
- 我为什么说 Python 是大数据全栈式开发语言
- 利用Python实现卷积神经网络的可视化
- 机器学习编程语言之争,Python夺魁
- 未来是人工智能的时代,更是Python的时代!
- Web 开发员 vs 数据科学家:谁将统治 Python?
- 核心代码从Python换成Go语言,提速30倍!
- 如何用Python解析HTML?
- 5种使用Python代码轻松实现数据可视化的方法
- 如何用一个Python示例入门TensorFlow?
- 小200行Python代码做了一个换脸程序
- 用Python构建你自己的RSS提示系统
- Python爬虫实践:《战狼2》豆瓣影评分析
- 3月编程排行榜:Python第四,R第20
- 全球删链?谷歌须在各国执行欧洲遗忘权
- 用Python构建一个极小的区块链
- 如何用Python创建眼下火爆的区块链?这篇干货请收藏