前言11月4日,中国消费者协会在官网发布消费提示,提醒消费者“双十一”购物六点注意事项 。主要内容就是对于双十一的“低价”不可迷信,提防商家套路 。那么对于我们要怎么样才能选择真正的底价好货呢?
今天带大家使用python+selenium工具获取这些公开的商家数据,可以采集商品的价格和评价做对比
环境介绍
- python 3.8
- pycharm
- selenium
- csv
- time
- random

文章插图
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))# 尽量避免人机检测随机延迟
对于本篇文章有疑问的同学可以加【资料白嫖、解答交流群:1136201545】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').textprice = div.find_element_by_xpath('.//strong').text + '元'deal = div.find_element_by_xpath('.//div[@class="deal-cnt"]').textname = div.find_element_by_xpath('.//div[@class="shop"]/a/span[2]').textlocation = div.find_element_by_xpath('.//div[@class="location"]').textdetail_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): # 012print(f'\n==================正在抓取第{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))# 尽量避免人机检测随机延迟最后运行代码,得到结果【Python模拟登录,采集淘宝商品数据 双十一手剁完了吗?教你用Python再剁一遍】

文章插图

文章插图

文章插图
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
