Python 相关学习

2020-09-18  本文已影响0人  温柔vs先生

运算符:

and x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 (a and b) 返回 20。
or  x or y  布尔"或" - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。    (a or b) 返回 10。
not not x   布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。    not(a and b) 返回 False
in  如果在指定的序列中找到值返回 True,否则返回 False。 x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in  如果在指定的序列中没有找到值返回 True,否则返回 False。   x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。
is  is 是判断两个标识符是不是引用自一个对象   x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is not  is not 是判断两个标识符是不是引用自不同对象   x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

is 与 == 区别:
is 用于判断两个变量引用对象是否为同一个(同一块内存空间), == 用于判断引用变量的值是否相等。

其他

例如,要导入模块 fib 的 fibonacci 函数,使用如下语句:

from fib import fibonacci
>>> import pickle
>>> my_list = [123, 12, "wbb"]
>>> pickle_file = open('my_list.pkl','wb')
>>> pickle.dump(my_list, pickle_file)
>>> pickle_file.close()
>>> pickle_file = open('my_list.pkl', 'rb')
>>> my_list2 = pickle.load(pickle_file)
>>> print(my_list2)
[123, 12, 'wbb']
>>> class Person:
    __name = 'wbb'
    def getName(self):
        return self.__name

    
>>> p = Person()
>>> p.__name
Traceback (most recent call last):
  File "<pyshell#81>", line 1, in <module>
    p.__name
AttributeError: 'Person' object has no attribute '__name'
>>> p.name
Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    p.name
AttributeError: 'Person' object has no attribute 'name'
>>> p.getName()
'wbb'
>>> p._Person__name
'wbb'
>>> 
>>> class C:
    def __init__(self, size = 10):
        self.size = size
    def getSize(self):
        return self.size
    def setSize(self, value):
        self.size = value
    def delSize(self):
        del self.size
    x = property(getSize, setSize, delSize)
    
>>> c1 = C()
>>> c1.x
10
>>> c1.x = 20
>>> c1.x
20
>>> c1.size
20
>>> del c1.x
>>> c1.x
Traceback (most recent call last):
  File "<pyshell#118>", line 1, in <module>
    c1.x
  File "<pyshell#111>", line 5, in getSize
    return self.size
AttributeError: 'C' object has no attribute 'size'
>>> 


>>> c = C(1)
>>> c
<__main__.C object at 0x0711C778>
>>> c.x
1
image
>>> class MyDecriptor:
    def __get__(self, instance, owner):
        print('getting...',self, instance, owner)
    def __set__ (self, instance, owner):
        print('setting...',self, instance, owner)
    def __delete__ (self, instance):
        print('deleting...',self, instance)

        
>>> class Test:
    x = MyDecriptor()

    
>>> test = Test()
>>> del test.x
deleting... <__main__.MyDecriptor object at 0x0149FB38> <__main__.Test object at 0x0149FAF0>
>>> test.x
getting... <__main__.MyDecriptor object at 0x0149FB38> <__main__.Test object at 0x0149FAF0> <class '__main__.Test'>
>>> test.x = '123'
setting... <__main__.MyDecriptor object at 0x0149FB38> <__main__.Test object at 0x0149FAF0> 123
>>> 
if __name__ == '__main__':
# 这里是测试代码
test()
improt 文件夹名字.模块名字 as 自定义的名字
import urllib.request
import re
import os
import ssl
ssl._create_default_https_context = ssl._create_unverified_context


def openHtml(url):
    request = urllib.request.Request(url)
    request.add_header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36")
    response = urllib.request.urlopen(request)
    html = response.read()
    
    return html


def find_imgs(url):
    html = openHtml(url).decode("utf-8")
    print(html)
    img_list = re.findall(r"http.+\.jpg",html)

    print(img_list)
    return img_list
    

def download_mm(folder = "xxoo"):
    url = "https://movie.douban.com/top250"

    os.mkdir(folder)
    os.chdir(folder)

    img_list = find_imgs(url)

    for img_url in img_list:

        with open(img_url.split("/")[-1], "wb") as f:
            img = openHtml(img_url)
            f.write(img)



if __name__ == "__main__":
    download_mm()

import urllib.request

url = "http://www.whatismyip.com.tw"

proxy_support = urllib.request.ProxyHandler({"http":"123.160.68.44:9999"})

opener = urllib.request.build_opener(proxy_support)

opener.add_headers = [("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36")]

urllib.request.install_opener(opener)

response = urllib.request.urlopen(url)

html = response.read().decode("utf-8")

print(html)
import requests


def download_mm():
    url = "https://movie.douban.com/top250"
    header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"}
    response = requests.get(url, headers = header)
    response.encoding = "utf-8"
    print(response.text)

    
if __name__ == "__main__":
    download_mm()
import requests
import os
from lxml import etree


def get_response(url):
    header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"}
    response = requests.get(url, headers = header)
    return response;
    
def find_imgs(url):
    response = get_response(url)
    response.encoding = "utf-8"
    html = response.text;

    #lxml 解析
    html = etree.HTML(html, etree.HTMLParser())
    
    img_list = html.xpath("//div[@id = 'wrapper']/div/div/div/ol/li/div/div[1]/a/img/@src")
    print(img_list)
    

    return img_list
    
def download_mm(folder = "xxoo"):
    url = "https://movie.douban.com/top250"

    if not os.path.exists(folder):
        os.mkdir(folder)
        os.chdir(folder)
    else:
        os.chdir(folder)

    img_lists = find_imgs(url)
    
    for img_url in img_lists:
        with open(img_url.split("/")[-1], "wb") as f:
            response = get_response(img_url)
            img = response.content
            f.write(img)
            f.close
    

    
if __name__ == "__main__":
    download_mm()

import requests
import os
from lxml import etree
import json


href_list = []
img_list = []
title_list = []


def save_image(url, f):
    header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"}
    response = requests.get(url = url, headers = header)
    img = response.content
    f.write(img)

# 找到主页面 list    
def find_elements(url, data):
    header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36","Content-Type": "application/json"}
    response = requests.post(url = url, data = json.dumps(data), headers = header)
    response.encoding = "utf-8"
    data_source = response.text
    data_source = json.loads(data_source)
    data_content = data_source["data"]
    
    #lxml 解析
    html = etree.HTML(data_content, etree.HTMLParser())

    global img_list, href_list, title_list
    
    img_list = html.xpath("//div[@class = 'post-module-thumb b2-radius']/a/img/@data-src")
    href_list = html.xpath("//div[@class = 'post-module-thumb b2-radius']/a/@href")
    title_list = html.xpath("//div[@class = 'post-info']/h2/a/text()")

    print(img_list,"\n--------",href_list,"\n--------",title_list)


def save_elements():
    for img_url in img_list:
        with open(img_url.split("/")[-1], "wb") as f:
            save_image(img_url, f)
    

    for idx, href in enumerate(href_list):
        header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"}
        response = requests.get(url = href, headers = header)
        response.encoding = "utf-8"
        data_source = response.text
        #lxml 解析
        html = etree.HTML(data_source, etree.HTMLParser())

        if not os.path.exists(title_list[idx]):
            os.mkdir(title_list[idx])
            os.chdir(title_list[idx])
        
            detail_img_list = html.xpath("//div[@class = 'entry-content']/div[2]/p/img/@src")

            if len(detail_img_list):
                for detail_img_url in detail_img_list:
                    str1 = detail_img_url.split("?")[0]
                    str2 = str1.split("/")[-1]
                    with open(str2, "wb") as f:
                        save_image(detail_img_url, f)

            os.chdir(os.path.abspath(os.path.dirname(os.getcwd())+os.path.sep+"."))




    
def download_mm(folder = "xxoo"):
    
    if not os.path.exists(folder):
        os.mkdir(folder)
        os.chdir(folder)
    else:
        os.chdir(folder)

        
    url = "https://www.jder.net/wp-json/b2/v1/getPostList"
    
    data = {}
    data["post_type"] = "post-1"
    data["post_order"] = "new"
    data["�post_row_count"] = "4"
    data["post_count"] = "24"
    data["post_thumb_ratio"] = "4/6"
    data["post_open_type"] = "0"
    data["post_paged"] = "1"
    data["post_load_more"] = "0"
    data["post_cat[0]"] = "meizi"
    data["show_sidebar"] = "0"
    data["width"] = "1100"
    data["paged"] = "2"


    find_elements(url, data)


    save_elements()
    

    
    
if __name__ == "__main__":
    download_mm()
获取单元格内容的数据类型
>>>> print sheet2.cell(1,0).ctype #第2行第1列:xiaoming2 为string类型 1
>>> print sheet2.cell(1,1).ctype #第2行第2列:12  为number类型 2
>>> print sheet2.cell(1,2).ctype #第2行第3列:2015/5/5 为date类型 3</pre>

说明:ctype : 0 empty,1 string, 2 number,** 3 date**, 4 boolean, 5 error

TODO:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 可写函数说明
def printinfo( arg1, *vartuple ):
   "打印任何传入的参数"
   print "输出: "
   print arg1
   for var in vartuple:
      print var
   return
 
# 调用printinfo 函数
printinfo( 10 )
printinfo( 70, 60, 50 )

下面是汉诺塔的方法,不太理解,待以后研究:


汉诺塔python.png
>>> a = [i for i in range(100) if not(i%2) and i%3]
>>> a
[2, 4, 8, 10, 14, 16, 20, 22, 26, 28, 32, 34, 38, 40, 44, 46, 50, 52, 56, 58, 62, 64, 68, 70, 74, 76, 80, 82, 86, 88, 92, 94, 98]

>>> b = {i:i%2==0 for i in range(10)}
>>> b
{0: True, 1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False}
>>> 
上一篇下一篇

猜你喜欢

热点阅读