計(jì)劃把每天學(xué)到的小知識(shí)記錄下來(lái)卑吭,就這樣吧
matlab:
如何加載.mat文件轉(zhuǎn)換為矩陣形式
S1 = load('IntBP1.mat');
BP1 = struct2cell(S1);
Mymat1 = cell2mat(BP1);
save Mymat1.mat Mymat1 -MAT
Mymat1就是矩陣形式了(S1是Struct Array 形式)。
cell:創(chuàng)建空的元胞數(shù)組
cellfun:為元胞數(shù)組的每個(gè)元胞執(zhí)行指定的函數(shù)
celldisp:顯示所有元胞的內(nèi)容
cellplot:利用圖形方式顯示元胞數(shù)組
cell2mat:將元胞數(shù)組轉(zhuǎn)變成為普通的矩陣
mat2cell:將數(shù)值矩陣轉(zhuǎn)變成為元胞數(shù)組
num2cell:將數(shù)值數(shù)組轉(zhuǎn)變成為元胞數(shù)組
deal:將輸入?yún)?shù)賦值給輸出
cell2struct:將元胞數(shù)組轉(zhuǎn)變成為結(jié)構(gòu)
struct2cell:將結(jié)構(gòu)轉(zhuǎn)變?yōu)樵麛?shù)組
iscell:判斷輸入是否為元胞數(shù)組
爬蟲:
如果爬取的是動(dòng)態(tài)信息抽诉,比如需要加載陨簇,翻頁(yè)等的信息吐绵,在Network中找對(duì)應(yīng)的進(jìn)行爬取迹淌,利用requests,bs4己单,json等庫(kù)唉窃;如果爬取的是靜態(tài)信息,如小說(shuō)圖片纹笼,則在Element中爬取纹份,利用requests,bs4等庫(kù)廷痘。
動(dòng)態(tài)信息用requests.get爬到后得用json()方法將response對(duì)象變?yōu)榱斜?字典對(duì)象蔓涧,再用取字典中的值的方法取出想要的信息。
requests庫(kù)用來(lái)取得網(wǎng)址內(nèi)容笋额,返回response對(duì)象元暴,可以用text方法提取到文字內(nèi)容,也可以用content提取到二進(jìn)制文件兄猩,如圖片茉盏,音樂(lè)鉴未。
r.status_code #響應(yīng)狀態(tài)碼
r.raw #返回原始響應(yīng)體,也就是 urllib 的 response 對(duì)象鸠姨,使用 r.raw.read() 讀取
r.content #字節(jié)方式的響應(yīng)體铜秆,會(huì)自動(dòng)為你解碼 gzip 和 deflate 壓縮
r.text #字符串方式的響應(yīng)體,會(huì)自動(dòng)根據(jù)響應(yīng)頭部的字符編碼進(jìn)行解碼
r.headers #以字典對(duì)象存儲(chǔ)服務(wù)器響應(yīng)頭讶迁,但是這個(gè)字典比較特殊连茧,字典鍵不區(qū)分大小寫,若鍵不存在則返回None
特殊方法#
r.json() #Requests中內(nèi)置的JSON解碼器
r.raise_for_status() #失敗請(qǐng)求(非200響應(yīng))拋出異常
bs4.BeautifulSoup可以用來(lái)解析requests.get得到的網(wǎng)址巍糯,雖然和.text方法print出的內(nèi)容看似一樣梅屉,但他們的類不同隧土,分別為<class 'str'> 與<class 'bs4.BeautifulSoup'>
import requests
from bs4 import BeautifulSoup
#引入BS庫(kù)
res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')
html = res.text
soup = BeautifulSoup(html,'html.parser') #把網(wǎng)頁(yè)解析為BeautifulSoup對(duì)象,BeautifulSoup中的第0個(gè)元素必須是字符串類型秽褒,第1個(gè)元素是解析器
一個(gè)常用的headers
headers={'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
res=requests.get(url,headers=headers)
exec的用法
例子
x = 10
expr = """
z = 30
sum = x + y + z
print(sum)
"""
def func():
y = 20
exec(expr, {'x': 1, 'y': 2}, {'y': 3, 'z': 4})
func()
-----------------輸出為----------------
34
上面這一串相當(dāng)于
x = 1
y = 2
def func():
y = 3
z = 4
z = 30
sum = x + y + z
print(sum)
func()
設(shè)計(jì)模式可以參考 https://blog.csdn.net/zhang1281480917/article/details/89530723
python的Template用法
- 字符串替換
template = "hello %s , your website is %s " % ("大CC","http://blog.me115.com")
print(template)
#等價(jià)于
template = "hello {0} , your website is {1} ".format("大CC","http://blog.me115.com")
print(template)
- 模板方式替換
from string import Template
tempTemplate = Template("Hello $name ,your website is $message") # 這種方式替換變量的標(biāo)志為$
print(tempTemplate.substitute(name='大CC',message='http://blog.me115.com'))
# 使用字典方式替換
from string import Template
tempTemplate = Template("Hello $name ,your website is $message") # 這種方式替換變量的標(biāo)志為$
a = {name:'大CC',message:'http://blog.me115.com'}
print(tempTemplate.substitute(a))
如果想換個(gè)替換方式的標(biāo)志塘砸,比如從$換為址愿!缀遍,可以這樣
from string import Template
class MyTemplate(Template):
delimiter = "!"
tempTemplate = MyTemplate("Hello !name ,your website is !message") # 這種方式替換變量的標(biāo)志為!
a = {name:'大CC',message:'http://blog.me115.com'}
print(tempTemplate.substitute(a))
jsonpath的使用
- jsonpath取值不需要查看json數(shù)據(jù)醒串,直接通過(guò)..(..就表示全局檢索后面跟的屬性)
data2 = jsonpath.jsonpath(json_data, "$..uname") # 全局搜索uname屬性帖鸦,如果沒(méi)有返回FALSE
print(data2)
- jsonpath是以列表的形式返回匹配到的值
repr和str區(qū)別
- 重構(gòu)repr方法后易遣,不管直接輸出對(duì)象(面向開(kāi)發(fā)者)還是通過(guò)print(面向用戶)咱筛,打印的信息都按我們repr方法中定義的格式進(jìn)行顯示
class Name:
def __init__(self,name):
self.name = name
def __repr__(self):
return 'Name: %s' % self.name
student = Name('jack')
student
print(student)
------------輸出--------------
Name: jack
Name: jack
- 重構(gòu)str后直接輸出對(duì)象(面向開(kāi)發(fā)者)顯示對(duì)象的內(nèi)存地址搓幌,使用print輸入對(duì)象顯示重構(gòu)后的格式
class Name:
def __init__(self,name):
self.name = name
def __str__(self):
return 'Name: %s' % self.name
student = Name('jack')
student
print(student)
-------------------輸出-------------------
<__main__.Name object at 0x0000020910CF0B20>
Name: jack
格式化時(shí)判斷
# 當(dāng)userid存在時(shí)拼接后面的格式化內(nèi)容,若不存在則不格式化{ userid and f' and userid={userid}'}
def sql(username, userid=""):
print(f"select * from userinfo where username='{username}'{ userid and f' and userid={userid}'}")
sql("test")
sql("test", 1)