Python爬蟲編程基礎5天速成(2022更新)Python入門+數(shù)據(jù)分析

python 學習(10天)
1.python基礎
(1)python:蟒蛇的意思;解釋型語言;面向對象;py3.0是主流;
TIOBE:編程語言流行程度排名統(tǒng)計。
(2)缺點:運行速度慢;代碼不能加密。
(3)典型應用
腳本、爬蟲、AI、WEB、數(shù)據(jù)分析、數(shù)據(jù)可視化
2.python環(huán)境
略
3.python
print("this is %d"%d)
\t space \n 換行
input()默認輸入為str類型。
強制類型轉換。
4.運算
in
not in
is
is not
5.條件
if :
elif :
else :
縮進很重要!每次都用TAB。
import random 引入隨機庫
random.randint(x,y)x和y之間(包括xy)的隨機整數(shù)。
6.import vs from import
practice:

運行結果:





代碼:import random Rand_number= random.randint(0,2) Usrinput=input("請輸入:剪刀(0)、石頭(1)、布(2):") if Usrinput.isdecimal(): #是否十進制數(shù) Usrinput=int(Usrinput); else:#不是則提示后退出 print("輸入錯誤!") exit() if Usrinput<0 or Usrinput >2: #是否在012的范圍內,不是則提示退出 print("輸入錯誤!") exit() if Usrinput == 0: #根據(jù)數(shù)字判斷出的是什么 print("你的輸入為:剪刀(0)") elif Usrinput == 1: print("你的輸入為石頭(1)") else: print("你的輸入為:布(2)") print("隨機生成數(shù)字:%d"%Rand_number) if Usrinput > Rand_number: print("恭喜你贏了") elif Usrinput < Rand_number: print("哈哈你輸了") else: print("平手")
7.for循環(huán)
(1)for i in range(5)
print(i)
output: 1 2 3 4 5
(2)for i in range(0,10,3)# for(i=0;i<10;i=i+3)
print(i)
output:0 3 6 9
(3)name ="chengdu"
for x in name:
print(x)
output:c h e n g d u
(4)a=["aa","bb","cc","dd"] 列表
for i in range(len(a))
print(i,a[i])
output:0 aa
1 bb
2 cc
3 dd
8.while
i=0
while i<5:
print("當前是第%d次循環(huán)"%(i+1))
print("i=%d"%i)
i+=1
【1-100求和】

while count <5:
print(count ,"<")
else:
print(count,">=")
8.break、continue、pass
break:出循環(huán)體
continue:跳過當前這一循環(huán)
pass:占位語句,沒用。
PRACTICE:

運行結果:

代碼:for i in range(1,10,1): for j in range(1,i+1,1): print("%d*%d=%d"%(i,j,i*j),end=" ") print()
字符串
string:單引號、雙引號、三引號。UTF-8編碼。支持拼接、截取。
word=‘字符串’
sentence="this is a sentence"
paragraph="""
this is a paragraph
"""
mystr='I\'m a student'
\+" 或者\+'

str=”chengdu“
print(str) chengdu
print(str[0:6]) # [起始位置(默認為0):結束位置(默認為字符長度):步進值]
chengd
print(str[1:7:2]) hnd
print(str[6:])
連接:
print(str + "你好")
print(str * 3)
output: chengduchengduchengdu
print(r"hello\nchengdu") #r消解轉義字符的功能
output:hello\nchengdu
常見操作(注意圖中加黑的):


判斷輸入是否非法:


List(列表)
1.類似數(shù)組但不是數(shù)組,可以把不同類型的混成一個數(shù)組。
2.0是開始值,-1是末尾值。
3.+連接,*表示重復。
4,逗號分隔開。
demo:
namelist=[] #定義一個空列表
namelist=["a","b","1"] #列表可以存儲混合的類型
print(namelist[0])
output:a
for i in namelist: #遍歷
print(name)
while i<length:
print(namelist[i])
i+=1
常用操作(增刪改查)


+:namelist.append(x) #末尾追加一個元素
如果x是一個列表,會被作為一個元素追加進去。
nameilist.extend(列表)#將列表中每個元素加進去。
insert:
namelist.insert(x,y) #把y插入到下標x的位置
delete:
del namelist[i] #刪除下標為i的元素
namelist.pop() #彈出末尾最后一個元素
namelist.remove("內容")#根據(jù)內容刪除!但是只能刪除第一個內容(如果有2個重復內容的話)
查找:
1.if...in...

2.index
namelist.index("object",start,end)#尋找object在下標start-end的范圍內,并返回一個下標。
范圍區(qū)間不包含end。(經(jīng)常左閉右開)
找不到會報錯。
3.namelist.count("c") #統(tǒng)計c出現(xiàn)了幾次
改變順序:
1.namelist,.reverse() #直接把原數(shù)組順序顛倒了
2.namelist.sort() #排序 升序
namelist.sort(reverse=true) #降序
嵌套:
namelist=[[],[],[]]#3個列表的空列表
namelist[0][0]

Practice:

運行結果:

代碼:
products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]] print("-------商品列表---------") i=0 for product in products: print("%d %s %d"%(i,product[0],product[1])) i=i+1 shopBasket =[] #購物籃 selectGoods=input("請輸入您想購入的商品編號,若無,按q可結算:") while(selectGoods!="q"): #是否為q if selectGoods.isdecimal(): #是否十進制數(shù) selectGoods=int(selectGoods) if selectGoods> len(products)-1:#超出范圍的序號 print("該商品不存在!") else: shopBasket.append(products[selectGoods]) else: print("非法輸入!") selectGoods=input("請輸入您想購入的商品編號,若無,按q可結算:") print("----------購物車----------") sum=0 for goods in shopBasket: print(goods)#結算 sum=goods[1]+sum print("總價",sum)
元組Tuple
不可以修改

tup1=() #create a null tuple
tup2 =(50,)#創(chuàng)建一個50開頭的元組,不然會以為tup2是一個整型變量(50)=50
example:
tup1=("abc","def",2000,2020)
print(tup1[0])
print(tup1[-1]) 訪問最后一個元素
print(tup1[1:5])(下標1-4)
output:
abd
2020
('def',2000,2020)(左閉右開)
增:
tup1=(12,34,56)
tup2=("ab","cd")
tup=tup1+tup2 #新開辟了一個空間
print(tup)
刪:
del tup1#刪除整個元組變量,無定義了,不能刪某一個值
修改:是不可能的
查:


字典(dict)鍵-值對

info = {"name":"a","age":"18"}
print(info["name"])
print(info["age"])
output:
a
18
訪問了不存在的鍵會報錯,可以采用不直接訪問的方式。
print(info.get("gender"))#get方法沒找到對應的鍵默認返回none
Info.get("gender","m")#沒找到時的默認值是m
output:
m
新增:
name["id"]=newID

刪除:
del info["name"]#刪除鍵值為name的整個鍵值對
del info #刪除了所有鍵值對
info.clear() #清空了鍵值對,但還能訪問Info
修改:
info["age"]=20
查:
1.查鍵
info.keys()

2.查值
info.values()
3.查項
info.items()
for key,value in info.items()
print("key=%s,value=%s"%(key,value))
enumerate(X,[start=0])
函數(shù)中的參數(shù)X可以是一個迭代器(iterator)或者是一個序列,start是起始計數(shù)值,默認從0開始。X可以是一個字典。


>>>dict() # 創(chuàng)建空字典
{}
>>> dict(a='a', b='b', t='t') # 傳入關鍵字
{'a': 'a', 'b': 'b', 't': 't'}

集合set(去重)


函數(shù)
def name(): #定義函數(shù)
print(".........")
1.有參數(shù)

2.有返回值

3.返回多個值

課堂練習:

全局變量和局部變量:

局部變量優(yōu)先使用,沒有局部變量使用全局。
如果非要使用全局的變量:global x

文件操作
f=open("test.txt","w") #寫,沒有則新建一個
f=open("test.txt","r")=f=open("test.txt")
rb:二進制讀
wb:二進制寫
content=f.read(5)#從第一個指針開始讀5個字符
content=f.readlines()#一次性讀取全部文件為列表,每行一個字符串元素


content=f.readline()#只能讀一行

f.close()
import os
os.rename("name","newname")
異常處理
try:
f=open("name.txt","r")
except IOError/NameError: #文件沒找到,屬于發(fā)生IO異常
pass #捕捉異常后應該執(zhí)行的代碼
異常類型想要被捕獲,需要一致
except (IOError,NameError)as result#將可能產(chǎn)生的所有異常都放到下面的小括號中
print("wrong")
print(result)

Exception可以承接任何異常

(1)try:
f=open("123.txt","r")
except Exception as result:
print("發(fā)生異常")
finally: #一定會被執(zhí)行
f.close()
print("文件關閉")
(2)

作業(yè):
def FileWrite(gushi,name): f=open(name,"w") f.write(gushi) f.close() def FileRead(name): try: f=open(name,"r") try: content=f.readlines() return content finally: f.close() except Exception as result: print(result) name="gushi.txt" gushi="春種一粒粟,秋收萬顆子。\n四海無閑田,農(nóng)夫猶餓死。\n鋤禾日當午,汗滴禾下土。\n誰知盤中餐,粒粒皆辛苦。" FileWrite(gushi,name) content=FileRead(name) name1="copy.txt" contenStr="".join(content) #用join()函數(shù)將list轉化為字符串 FileWrite(contenStr,name1) FileRead(name1)
爬蟲
前置知識:html+css+jsp
1.執(zhí)行入口:

2.引入模塊




urllib:
1.request(GET方法)
response=urllib.request.urlopen("http://www.baidu.com")
return response(網(wǎng)頁源代碼)
print(response.read().decode('utf-8'))//對獲取到的網(wǎng)頁源碼進行UTF-8解碼
2.(POST方法)模擬用戶真實登錄時使用
一個測試網(wǎng)站:httpbin.org
import urllib.parse
data= bytes(urllib.parse.urlencode{"hello":"world"}),encoding="utf-8")
response=urllib.request.urlopen("http://httpbin.org/post")
print(response.read().decode('utf-8'))//對獲取到的網(wǎng)頁源碼進行UTF-8解碼

3.
try: #超時處理
response= urllib.request.urlopen("url",timeout=0.01)
except urllib.error.URLError as e:
print("time out")
4.簡單的解析:
print(response.status)
print(response.getheaders())
print(response.getheader("Server"))
5.偽裝瀏覽器
url="..."
headers="User-Agent:......."#把正常瀏覽器訪問的header復制過來
data=bytes(urllib.parse.urlencode({"name":"eric"}),encoding="utf-8")
req = urllib.Request(url=url,data=data,headers=headers,method="POST")
response = urllib.request.urlopen(req)
print(response.read().decode("UTF-8"))
