工作過程中,經常會用python 執行一些自動化,腳本操作。非Python專業大頭兵,最近匯總的一些常用操作。
1. 如何在list中查找
for x in []:
if x meet condition:
#do sone action
2. 字符串轉換成數字
s=”99”
int(s)
3. python 生產隨機數
import random
random.randint(1, 50) #1~50之間的隨機數
4. 創建文件夾
os.mkdir()
創建子目錄
os.makedirs()
os.makedirs('temp/1/1/')
5. 檢查文件夾是否存在
os.path.exists
6. 檢查文件夾是否存在
枚舉目錄
os.walk(dir)
def enumDirs(dir):
abs_files=[]
for root, dirs, files in os.walk(dir):
for file in files:
s=os.path.join(root, file)
print(f'abs file={s}')
abs_files.append(s)
pass
或者
os.listdir(dir) #只能獲取檔次dir的所有文件,文件夾
7. list獲取文件絕對路徑
listdir(dir)
for item in dir:
absPath=os.path.join(dir, item)
#absPath 可以用
8. 刪除子目錄,還有子文件
os.removedirs(dir) #依賴沒有子文件
shutil.rmtree(dir) #直接全部干掉
9. 檢查是否是一個文件
os.path.isfile(filename)
10. 生產一個隨機數
import random
random.randint(0, 9) #生成0,9之間的隨機數
11. 讀寫txt文件
按行讀取文件
with open(filename, ‘r’, encoding=’utf-8’) as file_to_read:
while True:
lines=file_to_read.readline()
if not lines:
break;
process_line(lines)
寫文件
with open(filename, ‘w’) as file_to_write:
file_to_write.write(‘this is a line’)
12. python unitest 傳遞參數
可以通過命令行傳遞參數,也可以使用環境變量來傳遞參數
windows
SET HOST=localhost
from os import environ
print(envirom[‘HOST’])
13. list拼接字符串
str=‘’
list=[‘123’,’2222’]
str.join(list)
#output 1232222
14. 日期字符串轉date
def str2date(s):
#2021-11-05 06:35:18.370
try:
return datetime.datetime.strptime(s,'%Y-%m-%d %H:%M:%S.%f')
except ValueError:
print('convert str to date failed.%s' %(s))
print(s)
sys.exit(-1)
return ''
15. timer
import threading
def func():
print(“timer cb”)
timer=threading.Timer(5, func)
timer的基本用法
傳遞參數
def func(*argc **kvgc):
print()
timer=threading.Timer(5, func, (), {“param1”:1})
16. 執行windows cmd命令
import subprocess
return subprocess.Popen(cmd, shell=True, stdout=None)
os.system(cmd)
執行并且返回output內容
def exec(cmd):
output=subprocess.getoutput(cmd)
return output
17. python 獲取html表單
import pandas as pd
simpsons=pd.read_html(‘xxxxx.html’)
只能獲取html里面有table的內容,不是div哦
18. 命令行參數click
經常寫腳本處理相關任務的時候,比如會用到命令行參數,可以用getopt庫,這里使用click庫,click使用起來非常方便,功能超級強大
@click.group()
def helper():
pass
@click.command(‘—type’)
def work():
pass
helper.add(work)
if __name__==“__main__”:
helper()
官方網址
https://click.palletsprojects.com/en/8.1.x/
19. 定義一個屬性
class ebook(Base):
__tablename__='book'
@property
def helper():title(self):
pass
#二進制方式讀取
With open(“filename.zip”, mode=”rb”) as zip_file:
context=zip_file.read()
21. 獲取文件大小
Import os
Statinfo=os.state(filename)
os.st_size
22. 獲取類名和方法名稱
logger.info("Enter %s:%s"%(self.__class__.__name__,sys._getframe().f_back.f_code.co_name))
logger.info("Leave %s:%s"%(self.__class__.__name__,sys._getframe().f_back.f_code.co_name))
23. 去掉字符串的前后空格換行
str.strip()
24. 查找整個單詞
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
a=findWholeWord(‘aaaa’)(str)
if a :
find it
else
No find
25. 字符串startswith
判斷字符串是否xxx開始
27. python logger的使用
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger=logging.getLogger(__name__)
不寫什么大型的代碼,直接貼代碼,打log
28. 好用的f
打印log,或者debug 的時候,很多中方法f用起來非常方便
s=‘helloworld’
print(f’string={s}’) # string=helloworl
29. 好用的pytest
如果寫的代碼多了,一般寫一些算法函數的時候,要么單獨搞個test來測試,或者簡單點,搞個單元測試。在工作目錄里面創建一個test_xxx.py文件,然后倒入你要測試的函數,類,直接寫測試方法。寫完之后在目錄執行pytest -s 就可以自動執行
備注:-s 會把print的語句輸出出來
近在處理一些和有關電影的工作,需要用到一些北美電影票房數據,而這部分數據最權威的網站當屬Box Office Mojo(以下簡稱BOM),于是就上去查看了一下。估計經常關注這個網站的盆友們都知道,這個網站最近進行了改版,網頁排版全面更新,還專門針對移動設備進行了優化(以前的網站頁面只有電腦版的),頁面雖然好看了不少,但卻少了很多數據,之前的網站幾乎所有數據都能查到,而現在則只能查到部分數據,有些數據則要到BOM Pro版才能查到,而這個服務是收費的。為了更好地使用數據,還想不花錢,那就只有自己動手豐衣足食,所以筆者就自己寫了個Python爬蟲,爬取了過去多年的票房數據。以下就以"北美電影票房每日票房數據"為例,介紹一下如何爬取,其他票房數據類似,只需修改少數代碼即可。
圖1. 要抓取的部分網頁的截圖
這個爬蟲程序完全采用Python語言完成,使用軟件為Anaconda 2019.10版(這個目前是最新版的,理論上其包含的各種Python庫也是最新的或接近最新的,所以下面的爬蟲程序在部分老版軟件上可能會出問題,如有問題請及時更新)。爬蟲程序主要包括兩部分:爬取并存儲數據,以及根據數據簡單繪制圖片。下面就一一講解一下。
一、爬取和存儲數據
# 首先把需要的包都導入進來。
import requests
import pandas as pd
import time
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
import pylab as mpl # 導入中文字體,避免顯示亂碼
# 這個是我們要用到的每日票房的URL,中間的%s是一會兒要替換的年份
urltemplate=r'https://www.boxofficemojo.com/daily/%s/?view=year'
#這個是數據保存的地方,放在了桌面的一個Excel文檔中,因為數據很少,所以根本用不到數據庫,Excel足以,當然這里也可以用CSV格式。這里我的路徑中包含中文,使用時沒有問題,如果大家出現問題,最好使用英文路徑。
fileLoc=r'C:\BoxOffice\Box Office Mojo票房爬蟲\Daily\daily-data.xlsx'
# 這個是爬蟲頭部,防止網站的反爬機制。
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
下面是爬蟲主體部分,這里有三點要說明,一是mode='a'這里,這個是0.25.1版pandas以后才有的功能,之前的老版本沒有這個功能;二是,不知道是不是我的網絡有問題,在爬取過程中有掉線的現象出現,所以在這里用了requests.ConnectionError來處理掉線問題;三是,用了一個小竅門,如果直接用pd.read_html(url)也可以讀取網頁中的數據,但這里先用requests讀取網頁,再把requests讀取的網頁代碼放入pd.read_html中,這樣既可避免網站的反爬蟲機制,也可以加快讀取速度,因為pd.read_html直接讀取網頁實在太慢了。
def scraper(file, headers, urltemp, year_start, year_end):
writer=pd.ExcelWriter(file, engine='openpyxl', mode='a')
for i in range(year_start, year_end+1):
url=urltemp % i
try:
r=requests.get(url, headers=headers)
if r.status_code==200:
source_code=r.text
df=pd.read_html(source_code)
df=df[0]
df.to_excel(writer, sheet_name=str(i), index=False)
time.sleep(2)
except requests.ConnectionError:
print('Can not get access to the %s year daily data now' % i)
return
writer.save()
writer.close()
scraper(fileLoc, headers, urltemplate, 1977, 2019)
因為網站只提供到最早1977年的數據,所以就把1977年到2019年數據都給抓下來。
圖2. 抓取的部分數據的截圖
二、根據數據簡單繪圖
# 下面這個str_to_datetime函數,是除掉數據Date列中一些不必要的文字,比如有些數據帶有“New Year’s Eve”字樣,要把這些東西去掉
def str_to_datetime(x):
if len(x) > 14:
temp=x.split('2019')
x=temp[0]+'2019'
return x
# 這個str_to_num函數是把“Top 10 Gross”列的數據都轉換成數值,因為這些數據從Excel讀取到pandas后,都是string格式的數據,要轉換成數值格式
def str_to_num(x):
x=x.replace('$', '')
x=x.replace(',', '')
x=int(x)
return x
table=pd.read_excel(fileLoc, sheet_name='2019')
data=table[['Date', 'Top 10 Gross']]
data['Date']=data['Date'].apply(str_to_datetime)
data['Top 10 Gross']=data['Top 10 Gross'].apply(str_to_num)
# 設置x軸和y軸的數據,x軸是時間數據,y軸是票房數據,其值太大,所以改小點,方便作圖
x=pd.to_datetime(data['Date'])
y=data['Top 10 Gross']/1000000
# 找出票房數據中最大的那個值和其在序列y中的位置,然后找出對應x序列的位置,也就是對應的哪一天
max_loc=y.idxmax()
max_y=y.max()
max_date=x.loc[max_loc]
# 設置相關參數
mpl.rcParams['font.sans-serif']=['SimHei'] # 設置為黑體字
fig=plt.figure(figsize=(16, 6.5))
# 生成axis對象
ax=fig.add_subplot(111) # 本例的figure中只包含一個圖表
ax.set_ylim([0, 200])
plt.tick_params(labelsize=13)
# 設置x軸為時間格式,這個要注意,否則x軸顯示的將是類似于‘796366’這樣的轉碼后的數字格式
ax.xaxis.set_major_formatter(mdate.DateFormatter('%Y-%m-%d'))
plt.xticks(pd.date_range(x[len(x)-1], x[0], freq='M'), rotation=90)
text=r'票房最高的一天是%s,其票房為%.2f億' % (max_date.date(), max_y/100)
plt.annotate(text, xy=(max_date, max_y), fontsize=14, \
xytext=(max_date+pd.Timedelta(days=10), max_y+10), \
arrowprops=dict(arrowstyle="->", connectionstyle="arc3"), \
xycoords='data')
plt.ylabel('票房/百萬美元', fontdict={'size':14})
plt.plot(x, y)
完成后這個圖片效果如下
圖3. 2019年北美票房每日數據圖
三、結語
上面這個爬蟲程序比較簡單,并沒有用到數據庫、多線程等復雜技術,我們更多地應該從所得到的數據中來挖掘更多的價值,筆者接下來會從這些數據中來分析一下好萊塢電影行業過去一年的發展,屆時會分享給大家,敬請期待。
在這,我們接續昨天的 Python 數據處理(二),將以 R 語言的形式,重新實現一遍同樣的數據提取操作
為什么我要這么做呢,其實我是這么想的:
下面進入正題
在 Python 爬蟲項目中,最常用的是 requests 模塊。
而在 R 中,我們使用 rvest 模塊,進行網頁解析以及獲取網頁內容。
# install "rvest" package
install.packages("rvest")
# library
library(rvest)
我們還是以昨天的鏈接為例
首先用 read_html 讀取網頁鏈接
然后用 html_text 讀取整個網頁內容,返回的是一個字符串
# 網頁鏈接
URL <- "http://rest.kegg.jp/get/cpd:C01290"
# 獲取 URL 網頁
res <- read_html(URL)
# 讀取網頁文本
text <- html_text(res)
# 將文本按行分割
# strsplit 返回的是長度為 1 的 list,因此,可以用 unlist 轉換為 character
line_list <- unlist(strsplit(text, split = '\n'))
# 新建空 list,用于存儲我們的數據
data <- list()
for (i in 1:length(line_list)) {
line <- line_list[i]
# 提取前 12 個字符,substr(x, start, stop)
# 提取 start,stop 指定的 x 中字符的起始和結束位置
prefix <- substr(line, 1, 12)
# 判斷是否包含字母數字
if (grepl("\\w+", prefix)) {
# 去除多余的空白字符
key <- sub(pattern = "\\s+", replacement = "", x = prefix)
}
# 獲取 line 中位置 13 到末尾的字符,nchar(x) 計算字符串 x 的長度
value <- substr(line, 13, nchar(line))
if (key == "ENTRY") {
# 在這,使用 perl 形式的正則 perl = TRUE,以多個空白字符分隔字符串
data$entry <- unlist(strsplit(value, split = "\\s+", perl = TRUE))[1]
} else if (key == "NAME") {
v <- substr(value, 1, nchar(value)-1)
data$name <- c(data$name, v)
} else if (key == "ENZYME") {
v <- unlist(strsplit(value, split = "\\s+", perl = TRUE))
data$enzyme <- c(data$enzyme, v)
} else if (key == "DBLINKS") {
v = unlist(strsplit(value, ": "))
data$dblinks[v[1]] <- v[2]
}
}
輸出提取的信息
> data
$entry
[1] "C01290"
$name
[1] "Lactosylceramide"
[2] "beta-D-Galactosyl-(1->4)-beta-D-glucosyl-(11)-ceramide"
[3] "beta-D-Galactosyl-1,4-beta-D-glucosylceramide"
[4] "Gal-beta1->4Glc-beta1->1'Cer"
[5] "LacCer"
[6] "Lactosyl-N-acylsphingosine"
[7] "D-Galactosyl-1,4-beta-D-glucosylceramid"
$enzyme
[1] "2.4.1.92" "2.4.1.206" "2.4.1.228" "2.4.1.274" "2.4.99.1" "2.4.99.9" "3.2.1.18" "3.2.1.22"
[9] "3.2.1.23" "3.2.1.47" "3.2.1.52"
$dblinks
PubChem ChEBI LIPIDMAPS LipidBank
"4509" "17950" "LMSP0501AB00" "GSG1147"
仔細看看代碼邏輯,和 Python 是很像的。
其中一些函數可以映射到 Python 中的函數,雖然他們之間用法、參數、返回值會有點差別,但是作用是類似的。
上面代碼中我們都是用 R 自帶的字符串操作,用起來比較麻煩,不是很得心應手。
后面,我會給大家講講 R 的字符串操作模塊 stringr,能極大提升我們開發效率。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。