python第6天作业

如何携带cookies  破解登录

r'''
步骤:
1、打开文件的查看,显示隐藏文件
2、找到C:\Users\administortra\AppData\Local\Google\Chrome\User Data
删除Default文件
3、 重新打开浏览器,并登陆百度账号 登录后关闭浏览器 运行代码
- 此时会创建一个新的Default缓存文件
'''
# 获取options对象,参数对象
options = ChromeOptions()

# 获取cookies保存路径
# 'C:\Users\administortra\AppData\Local\Google\Chrome\User Data'
profile_directory = r'--user-data-dir=C:\Users\administortra\AppData\Local\Google\Chrome\User Data'

# 添加用户信息目录
options.add_argument(profile_directory)

# 把参数加载到当前驱动中  chrome_options默认参数,用来接收options对象
driver = webdriver.Chrome(chrome_options=options)

try:
    driver.implicitly_wait(10)
    driver.get('https://www.baidu.com/')
    '''
    每一次登陆的百度都会产生不一样的bduss  需要重新获取
    BDUSS:FOVE5lUHdYTGpiSVYxc0JZOW9FVkVWWVFGenlIdmRlSHVpWmJYMFI2cDI1QzlkRVFBQUFBJCQAAAAAAAAAAAEAAABP1SKlzqjUuLTLyfrO3gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHZXCF12VwhdOV
    '''
    # 添加用户cookies信息
    # name、value必须小写
    driver.add_cookie({"name": "BDUSS", "value": "F3T25WZHBxWXBMR0tjbVh4MkM4YU5CVnhVU1dRMFRZZEZCWm04WnE3ZE41aTlkRVFBQUFBJCQAAAAAAAAAAAEAAABP1SKlzqjUuLTLyfrO3gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE1ZCF1NWQhdU0"})

    # 刷新操作
    driver.refresh()
    print(11111)
    time.sleep(10)

finally:
    driver.close()

  

 

自动登录抽屉网

from selenium import webdriver
import time

driver = webdriver.Chrome(r'D:\BaiduNetdiskDownload\chromedriver_win32\chromedriver.exe')

# 把窗口转成全屏
driver.maximize_window()

try:
    driver.get('https://dig.chouti.com/')
    driver.implicitly_wait(10)
    time.sleep(5)

    # 1、点击登录
    login_btn = driver.find_element_by_id('login_btn')
    login_btn.click()
    time.sleep(2)

    # 2、输入用户名
    phone = driver.find_element_by_class_name('login-phone')
    phone.send_keys('15622792660')

    # 3、输入密码
    pwd = driver.find_element_by_class_name('pwd-password-input')
    pwd.send_keys('kermit46709394')

    # 4、确认登录
    login_submit = driver.find_element_by_class_name('btn-large')
    login_submit.click()

    time.sleep(20)

# 捕获异常并打印
except Exception as e:
    print(e)

finally:
    driver.close()

     

爬取京东商城信息

'''
爬取京东商品信息:
请求url:
https://www.jd.com/
提取商品信息:
1.商品详情页
2.商品名称
3.商品价格
4.评价人数
5.商品商家

'''
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


def get_good(driver):
    try:

        # 通过JS控制滚轮滑动获取所有商品信息
        js_code = '''
            window.scrollTo(0,5000);
        '''
        driver.execute_script(js_code)  # 执行js代码

        # 等待数据加载
        time.sleep(2)

        # 3、查找所有商品div
        # good_div = driver.find_element_by_id('J_goodsList')
        good_list = driver.find_elements_by_class_name('gl-item')
        n = 1
        for good in good_list:
            # 根据属性选择器查找
            # 商品链接
            good_url = good.find_element_by_css_selector(
                '.p-img a').get_attribute('href')

            # 商品名称   .p-name em   表示p—name下  em标签
            good_name = good.find_element_by_css_selector(
                '.p-name em').text.replace("\n", "--")

            # 商品价格
            good_price = good.find_element_by_class_name(
                'p-price').text.replace("\n", ":")

            # 评价人数
            good_commit = good.find_element_by_class_name(
                'p-commit').text.replace("\n", " ")

            good_content = f'''
                        商品链接: {good_url}
                        商品名称: {good_name}
                        商品价格: {good_price}
                        评价人数: {good_commit}
                        \n
                        '''
            print(good_content)
            with open('jd.txt', 'a', encoding='utf-8') as f:
                f.write(good_content)

        next_tag = driver.find_element_by_class_name('pn-next')
        next_tag.click()

        time.sleep(2)

        # 递归调用函数
        get_good(driver)

        time.sleep(10)

    finally:
        driver.close()


if __name__ == '__main__':

    good_name = input('请输入爬取商品信息:').strip()

    driver = webdriver.Chrome()
    #driver.implicitly_wait(10)
    # 1、往京东主页发送请求
    driver.get('https://www.jd.com/')

    # 2、输入商品名称,并回车搜索
    input_tag = driver.find_element_by_id('key')
    input_tag.send_keys(good_name)
    input_tag.send_keys(Keys.ENTER)
    time.sleep(2)

    get_good(driver)

  

元素交互操作
from selenium import webdriver
from selenium.webdriver import ActionChains
import time

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')

try:

    # driver.switch_to_frame('iframeResult')
    # 切换到id为iframeResult的窗口内
    driver.switch_to.frame('iframeResult')

    # 源位置
    draggable = driver.find_element_by_id('draggable')

    # 目标位置
    droppable = driver.find_element_by_id('droppable')

    # 调用ActionChains,必须把驱动对象传进去
    # 得到一个动作链对象,复制给一个变量
    actions = ActionChains(driver)

    # 方式一: 机器人
    # 瞬间把源图片位置秒移到目标图片位置
    # actions.drag_and_drop(draggable, droppable)  # 编写一个行为
    # actions.perform()  # 执行编写好的行为


    # 方式二: 模拟人的行为
    source = draggable.location['x']
    target = droppable.location['x']
    print(source, target)

    distance = target - source
    print(distance)

    # perform:每个动作都要调用perform执行

    # 点击并摁住源图片
    ActionChains(driver).click_and_hold(draggable).perform()

    s = 0
    while s < distance:
        # 执行位移操作
        ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
        s += 2

    # 释放动作链
    ActionChains(driver).release().perform()

    time.sleep(10)


finally:
    driver.close()

  



转载于:https://www.cnblogs.com/xiaohuangxiong/p/11067971.html

你可能感兴趣的:(python第6天作业)