Python 小demo 及 小记

1.小记1  (c1.py)

# 表达式

# 1.表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列。
# 2.若表达式有赋值符号(=),就要考虑右结合;如果没有赋值符号,就是左结合。

# a = 1
# b = 2
# c = 3
# print(a + b * c)            #7 (*号的优先级大于+)
# print(a or b and c)         #1 (and的优先级大于or)
# d = a or b
# print(d)                    #1 (or的优先级高于=)

# 3.优先级
    # 3.1 算数  >  比较  >  逻辑(not>or)
    # 3.2 逻辑运算符的优先级  (not > and >or)
    # 3.3括号能辅助理解优先级;括号还能改变优先级关系
# a = 1
# b = 2
# c = 2
# d = (not a) or ((b + 2) == c)
# print(d)

# 4.推荐编辑器
    # pycharm,vscode,sublime
    # IDE Integrated Development Enviroment

# 5.vscode
    # 5.1字体(等基础设置)设置 
        # 文件  ==》 首选项  ==》  设置
    # 5.2保存为.py文件,通过  ctrl+` 快捷键打开命令行, 关闭命令行也是通过  ctrl+`
    # 5.3也可通过  右键  ==》  在终端打开
    # 5.4智能感知,断点调试
    # 5.5建议安装的插件  python、Terminal、vscode-icons
        # python:智能感知,python语法编写规范提示
        # Terminal:使在vscode中写的代码能同步到别的编辑器,例如在vscode中写小程序的代码,在小程序编辑器中看写后的效果。
        # vscode-icons:会安装一些美化的icon图标
    # 5.6重命名,  选中文件  ==>  再 按F2
    # 5.7一个tab等于几个空格设置   文件  ==》 首选项  ==》  设置
    # snippet  片段  (大部分IDE都有)   (快速构建代码片段)

# 6.Python语法的一些规范
    # 6.1末尾无需分号(;)
    # 6.2流程控制语句以及类和函数的定义;
        # 无需{}来包裹一个代码块,Python中是靠缩进来区分代码和代码块
        # 流程控制语句:    条件控制,      循环控制,         分支
        #                   if else         for while
    # 6.3注释
        # 6.3.1单行注释 通过 # 井号来实现; (在vscode中通过快捷键  ctrl+/ )
        # 6.3.2多行注释通过(""" 这里写注释内容 """)一对三引号; (在vscode中通过快捷键  alt+shift+a )

# mood = True
# if mood :
#     print('go left')
# else:
#     print('go right')

    # 6.4在条件语句中,
        # 6.4.1没有{}来包裹代码块;
        # 6.4.2是通过4个空格来区分代码块的;
        # 6.4.3条件结束,通过冒号结束(语法的标识符),建议这冒号前不加空格;
        # 6.4.4具有相同的缩进的代码块,视为统一代码块
        # 6.4.5所以Python代码不能进行压缩;
            # 现在在云服务的时代(代码都托管在自己的服务器上,也不存需要加密防止别人破译)
            # 商业授权(桌面应用程序),就需要加密
        # 6.4.6 if 后边的条件还可是一个条件表达式(只要条件表达式可以转为一个bool值即可)
# a = 1
# b = 2
# c = 2
# if a or b + 1 == c :
#     print('go left')
# else:
#     print('go right')

        # 6.4.7 if 可以单独使用,else不能单独使用
        # 6.4.8 if 中可以进行if嵌套
        # 6.4.9 如果当if中的代码块的内容比较多的话,应该考虑分装为一个函数,用这个封装过的函数来代替,这样代码会比较清爽;

    # 6.5变量(小写,推荐单词之间通过下划线连接)
        # constant (常量),在Python中没有真正意义上的常量,但对于常量建议将所有字母大写
        # module(模块)
    # 6.6在运算符和变量之间有空格,在else语句结束后再来一个空行
    # 6.7 pass 空语句,占位语句
         
# 7.# 优先级:算数(+,-,*,/,%,//,**)  >  比较(关系,==,+=)  >  逻辑(not > and > or)

2.小demo  (c2.py)

# 模拟用户输入账号密码

account = 'qiyue'
password = '123456'

print('please input account')
user_account = input()
print('please input password')
user_password = input()

if account == user_account and password == user_password:
    print('success')
else:
    print('fail')

3.小记2  (c3.py)

# 1.apple orange banana shopping
# 输入的input里的是str,需要转一下类型
# a = input()
# print(a)
# print(type(a))
# b = int(a)

# if b == 1:
#     print('apple')
# elif b == 2:
#     print('orange')
# elif b == 3:
#     print('banana')
# else:
#     print('shopping')

# 2.d 或 e不可能同时为False,返回d 和 e中的为真的那个值
d = 1
e = 0
c = ''
    # 2.1一般思路,都会想着用if else 
# if d:
#     c = d
# else:
#     c = e

    # 2.2在Python中还有or运算符,比if else 更方便
# c = d or e

(备注:以上内容来自七月老师的学习笔记,仅作为学习使用)

文件: import scrapy from demo1.items import Demo1Item import urllib from scrapy import log # BOSS直聘网站爬虫职位 class DemoSpider(scrapy.Spider): # 爬虫名, 启动爬虫时需要的参数*必填 name = 'demo' # 爬取域范围,允许爬虫在这个域名下进行爬取(可选) allowed_domains = ['zhipin.com'] # 爬虫需要的url start_urls = ['https://www.zhipin.com/c101280600/h_101280600/?query=测试'] def parse(self, response): node_list = response.xpath("//div[@class='job-primary']") # 用来存储所有的item字段 # items = [] for node in node_list: item = Demo1Item() # extract() 将xpath对象转换为Unicode字符串 href = node.xpath("./div[@class='info-primary']//a/@href").extract() job_title = node.xpath("./div[@class='info-primary']//a/div[@class='job-title']/text()").extract() salary = node.xpath("./div[@class='info-primary']//a/span/text()").extract() working_place = node.xpath("./div[@class='info-primary']/p/text()").extract() company_name = node.xpath("./div[@class='info-company']//a/text()").extract() item['href'] = href[0] item['job_title'] = job_title[0] item['sa 报错: C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\python.exe "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\pydevconsole.py" --mode=client --port=55825 import sys; print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend(['C:\\Users\\xieqianyun\\demo1', 'C:/Users/xieqianyun/demo1']) Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 7.10.0 -- An enhanced Interactive Python. Type '?' for help. PyDev console: using IPython 7.10.0 Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32 runfile('C:/Users/xieqianyun/demo1/demo1/begin.py', wdir='C:/Users/xieqianyun/demo1/demo1') Traceback (most recent call last): File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-fc5979762143>", line 1, in <module> runfile('C:/Users/xieqianyun/demo1/demo1/begin.py', wdir='C:/Users/xieqianyun/demo1/demo1') File "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Users\xieqianyun\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/Users/xieqianyun/demo1/demo1/begin.py", line 3, in <module> cmdline.execute('scrapy crawl demo'.split()) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\cmdline.py", line 145, in execute cmd.crawler_process = CrawlerProcess(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 267, in __init__ super(CrawlerProcess, self).__init__(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 145, in __init__ self.spider_loader = _get_spider_loader(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\crawler.py", line 347, in _get_spider_loader return loader_cls.from_settings(settings.frozencopy()) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 61, in from_settings return cls(settings) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 25, in __init__ self._load_all_spiders() File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\spiderloader.py", line 47, in _load_all_spiders for module in walk_modules(name): File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\scrapy\utils\misc.py", line 73, in walk_modules submod = import_module(fullpath) File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "C:\Users\xieqianyun\demo1\demo1\spiders\demo.py", line 4, in <module> from scrapy import log ImportError: cannot import name 'log'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值