pdb命令调试python程序

1)进入命令行Debug模式,python -m pdb xxx.py
    2)h:(help)帮助
    3)w:(where)打印当前执行堆栈
    4)d:(down)执行跳转到在当前堆栈的深一层(个人没觉得有什么用处)
    5)u:(up)执行跳转到当前堆栈的上一层
    6)b:(break)添加断点
                 b 列出当前所有断点,和断点执行到统计次数
                 b line_no:当前脚本的line_no行添加断点
                 b filename:line_no:脚本filename的line_no行添加断点
                 b function:在函数function的第一条可执行语句处添加断点
    7)tbreak:(temporary break)临时断点
                 在第一次执行到这个断点之后,就自动删除这个断点,用法和b一样
    8)cl:(clear)清除断点
                cl 清除所有断点
                cl bpnumber1 bpnumber2... 清除断点号为bpnumber1,bpnumber2...的断点
                cl lineno 清除当前脚本lineno行的断点
                cl filename:line_no 清除脚本filename的line_no行的断点
    9)disable:停用断点,参数为bpnumber,和cl的区别是,断点依然存在,只是不启用
    10)enable:激活断点,参数为bpnumber
    11)s:(step)执行下一条命令
                如果本句是函数调用,则s会执行到函数的第一句
    12)n:(next)执行下一条语句
                如果本句是函数调用,则执行函数,接着执行当前执行语句的下一条。
    13)r:(return)执行当前运行的函数直到结束。即执行到函数返回
    14)c:(continue)继续执行,直到遇到下一条断点
    15)l:(list)列出源码
                 l 列出当前执行语句周围11条代码
                 l first 列出first行周围11条代码
                 l first second 列出first--second范围的代码,如果second<first,second将被解析为行数
    16)a:(args)列出当前执行函数的函数
    17)p expression:(print)输出expression的值
    18)pp expression:好看一点的p expression
    19)run:重新启动debug,相当于restart
    20)q:(quit)退出debug
    21)j lineno:(jump)设置下条执行的语句函数
                只能在堆栈的最底层跳转,向后重新执行,向前可直接执行到行号
    22)unt:(until)执行到下一行(跳出循环),或者当前堆栈结束
    23)condition bpnumber conditon,给断点设置条件,当参数condition返回True的时候bpnumber断点有效,否则bpnumber断点无效
    24)bt : 查看调用堆栈信息

在程序中需要打断点处添加 pdb.set_trace()  ,这样程序会在 pdb.set_trace() 暂停并进入 pdb 调试环境,可以用 “p 变量名” 查看变量,或者 c 继续运行

命令行直接输入命令:python mao_yan_cookies.py,然后 回车

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author      : 
# @File        : mao_yan_cookies.py
# @Software    : PyCharm
# @description : XXX
 
 
import pdb
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import selenium.webdriver.support.ui as ui
 
# from selenium.webdriver.common.action_chains import ActionChains
 
# chrome_options = webdriver.ChromeOptions()
# # chrome_options.add_argument('--headless')
# browser = webdriver.Chrome(chrome_options=chrome_options)
# # 打开浏览器 设定等待加载时间 访问URL
# wait = ui.WebDriverWait(browser, 10)
 
 
# def test_1():
#     browser.get('https://www.baidu.com/')
#     print('打开浏览器')
#     print(browser.title)
#     browser.find_element_by_id('kw').send_keys('测试')
#     print('关闭')
#     browser.quit()
#     print('测试完成')
#
#
# def test_2():
#     url = "https://www.newrank.cn/public/info/list.html?period=pgcweek&type=data"
#     browser.get(url)
#     print(browser.page_source)  # 打印渲染后的页面代码
#
#
# def test_3():
#     url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
#     browser.get(url)
#     browser.switch_to.frame('iframeResult')  # 切换到 嵌套的 html 页面
#     source = browser.find_element_by_css_selector('#draggable')
#     print(source)
#     try:
#         logo = browser.find_element_by_class_name('logo')
#     except NoSuchElementException:
#         print('NO LOGO')
#     browser.switch_to.parent_frame()  # 返回到父 html 页面
#     logo = browser.find_element_by_class_name('logo')
#     print(logo)
#     print(logo.text)
 
 
class MaoYanCookies(object):
    def __init__(self):
        super(MaoYanCookies, self).__init__()
        chrome_options = webdriver.ChromeOptions()
        # chrome_options.add_argument('--headless')
        self.browser = webdriver.Chrome(chrome_options=chrome_options)
        # 打开浏览器 设定等待加载时间 访问URL
        self.wait = ui.WebDriverWait(self.browser, 10)
        pass
 
    def get_cookies(self):
        cinema_url = 'http://maoyan.com/cinema/15280'
        self.browser.get(cinema_url)
        base_time_element = self.browser.find_element_by_xpath('//table[@class="plist"]//tbody//td//a')
        base_time_url = base_time_element.get_attribute('href')
        print(base_time_url)
        self.browser.get(base_time_url)
        cookies = self.browser.get_cookies()
        print(cookies)
        pass
 
    def __del__(self):
        self.browser.close()
 
 
if __name__ == "__main__":
    # test_1()
    # test_2()
    # test_3()
    pdb.set_trace()   # <-- Break point added here,设置的断点
    myc = MaoYanCookies()
    myc.get_cookies()
    pass

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值