爬虫(四)selenium添加购物车(cookies)

news/2024/5/18 15:45:55 标签: selenium, cookie, By.CSS, expected_conditions, WebDriverWait

上一篇关于selenium的文章其实只是通过它来得到网页代码,然后用BeautifulSoup来进行信息的查找。

这一次也不复杂,只是通过selenium自带的CSS选择器来选择按钮,然后点击选入购物车。这次模拟的是抢购的场景,也就是设定时间,然后自行选购。(emmm,好吧,就是按键精灵低级版,虽然我也没用过按键精灵)然后稍微运用了下cookies。

因为这次代码真的不复杂。所以不打算仔仔细细的记笔记。只不过调用的函数比较多,在代码里面注释下就OJBK好了。

selenium的简单调用可以看我之前的文章:selenium爬二手房

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time
import json


def get_url(url, time_):
    chrome_options = webdriver.ChromeOptions()
    # 这里取消注释的话,会不显示浏览器出来。需要Chrome59之后的版本
    # chrome_options.add_argument('--headless')

    browser = webdriver.Chrome(chrome_options=chrome_options, executable_path='C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe')
    # 这里设置wait之后,后面在执行代码之后超过十秒将抛出异常
    wait = WebDriverWait(browser, 10)

    try:
        browser.get(url)
        # 在这里打印cookies跟后面的对比
        temp = browser.get_cookies()
        print(temp)
        browser.delete_all_cookies()
        try:
            with open('cookie.json', 'r') as file:
                cookies = json.load(file)
                for cookie in cookies:
                    browser.add_cookie(cookie)
                print('loading cookies')
        except IOError:
            print('No cookie file found')
        print('reload the url')
        browser.get(url)
        temp = browser.get_cookies()
        print(temp)
        # log in 
        # 我是自己通过扫码登陆...比起自己检测窗口然后输入账号密码,我觉得这样快点
        # 所以在第一次执行代码的时候,这个sleep(60)的时间是来给我登陆,然后记录登陆后的cookies
        time.sleep(60)
        cookie_list = browser.get_cookies()
        # 发现登陆后的cookie改变了,然后写入文件,主要训练下自己对cookies的使用
        print(cookie_list)
        with open('cookie.json', 'w') as file:
            json.dump(cookie_list, file)
            print('Downloading the cookie')
        # 这里是等待按钮的出现。通过CSS选择器,接着使用EC来检测按钮的出现,等待时间是上面设置的10秒
        # 如果10秒内按钮没有出现,将抛出异常。如果出现,那button就是购物车那个按钮
        button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a#InitCartUrl.btn-special1.btn-lg')))
        print('Button found!')
        # 等待自己设置的时间
        fit_time(time_)
        button.click()
        print('Good job!')
        print(time.localtime())
        # 不Sleep的话,因为一点击就结束程序,浏览器马上关掉,就不能够加入购物车。
        time.sleep(600)
    except TimeoutException:
        print('Timeout')


def fit_time(fittime):
    now = time.localtime()
    now = now[0:3] + fittime + now[-3:]
    now = time.mktime(now)
    timestamp = time.time()
    time.sleep(now-timestamp)


if __name__ == '__main__':
    target_url = 'https://item.jd.hk/1968268294.html'
    target_time = (23, 42, 0)
    get_url(target_url, target_time)

总体就这样,主要训练下自己对于selenium的一些操作和对cookies的使用,毕竟是第一次用。

想要继续接触下简单的数据库接着再学习框架。


http://www.niftyadmin.cn/n/1364260.html

相关文章

JDK之Enumeration

源码 package java.util;/*** author Lee Boynton* since JDK1.0*/ public interface Enumeration<E> {boolean hasMoreElements();E nextElement(); }Enumeration的遍历 Spring之ContextCleanupListener源码 package org.springframework.web.context;import java…

html中设置默认的浏览器文档模式

<head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><TITLE>${systemConfig.getStringConfigValue("webtitle")}——登陆</TITLE><meta name"description" content"" /&…

制作简易词典(PyQt5+爬虫)

在查看剑桥词典网页版的时候&#xff0c;逼着自己看英英&#xff0c;但有时候看不懂还得打开英汉的页面。 然后&#xff0c;就自己查了点界面的知识&#xff0c;结合前面学的一点爬虫&#xff0c;捣鼓了一下。 放在这里~~~ 先说明&#xff0c;我也是新手。 from PyQt5.QtWi…

JAVA实现寻找两个有序数组的中位数

介绍 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 请你找出这两个有序数组的中位数&#xff0c;并且要求算法的时间复杂度为 O(m n) 你可以假设 nums1 和 nums2 不会同时为空 源码 public static double getMedianSortedArrays(int[] nums1, int[] nums2) {int lengt…

Pyinstaller闪退补救措施

用Pyinstaller封装一个py文件时&#xff0c;过程没有问题&#xff0c;但打开后会出现闪退的现象。因此上网找&#xff0c;找到了上面网址所示的解决方法。也就是用cmd打开exe文件。 假如我要封装mani.py&#xff0c;Windows下进入文件所在地址&#xff0c;按住shift点击鼠标右…

对移植工程的makefile理解。

本文的目的已在对于makefile结构和其参数的理解。 该工程的编译类型为交叉编译。 工程一共包含5个makefile文件&#xff0c;分别是&#xff1a; ./makefile ./api/compile.cfg ./api/archdef.cfg ./api/rules.cfg ./sudir.cfg 还有一个makefile中的include文件为&#xff1a;./…

MyBatis-Spring之SqlSessionFactoryBean

前言 Spring集成MyBatis时&#xff0c;需要mybatis-3.3.0.jar、mybatis-spring-1.2.3.jar Spring Boot集成MyBatis时&#xff0c;需要mybatis-3.3.0.jar、mybatis-spring-1.2.3.jar、mybatis-spring-boot-autoconfigure-1.3.1.jar MyBatis-Spring官方文档 在基础的 MyBatis 用法…

(PyQt5)垂直布局控件混乱,QMainWindow要背锅

自己写好的小字典程序想要更改绝对布局的方式&#xff0c;改成BoxLayout。但是屡屡不行&#xff0c;浪费了很多时间。 只能复制代码过来一个个试探哪里错&#xff0c;发现影响的不是代码&#xff0c;而是继承的类。 在网上搜到的关于布局的代码都是QWdiget类&#xff0c;但是…