python教程第二講 Python爬蟲 第二講

2021-10-19 21:33:30 字數 4364 閱讀 6260

for迴圈

name = 'neusoft'

for x in name:

print(x)

if x == 's':

print('哈哈')

# 結果

>>> neu

s哈哈of

t進度條

使用 pip install tqdm 安裝tqdm庫

匯入 tqdm 庫和 time 庫

from tqdm import tqdm

import time

mylist =

for i in range(20):

# 遍歷mylist

for x in tqdm(mylist):

time.sleep(1)

# 結果

>>> 35%|███▌ | 7/20 [00:07<00:13, 1.01s/it]

字串操作

字串的替換

# 常用操作

price = '¥9.9'

# 字串的替換

price = price.replace("¥", '')

print(price)

# 結果

>>> 9.9

# **漲價 10倍

new_price = float(price) *10

print(new_price)

# 結果

>>> 99.0

while true:

seg = input('')

seg = seg.replace('嗎?', '!')

print(seg)

# 結果

>>> 你好嗎?

>>> 你好!

strip 去空格操作

name = ' neuedu '

print(len(name))

# 結果

>>> 13

name = name.strip()

print(len(name))

# 結果

>>> 6

join 將列表變成字串

li = ['你', '好', '帥']

disk_path = ['c:','users', 'administrator', 'desktop', 'ccf']

path = '\\'.join(disk_path)

print(path)

# 結果

>>> c:\users\administrator\desktop\ccf

li = ''.join(li)

print(li)

# 結果

>>> 你好帥

元組元組的作用

寫保護 ,安全, python內建函式返回的型別都是元組

相對列表來講, 元組更節省空間, 效率更高

建立元組

# 元組和列表很相似,只不過不能修改

a = (1, '1', 3)

print(a)

print(type(a))

# 結果

>>> (1, '1', 3)

# 擁有乙個元素的元組

a = (100)

print(type(a))

# 結果

b = (100,)

print(type(b))

# 結果

# 我們經常使用的組合方式:

list2 = [('a',22),('b', 33),('c',99)]

##########訪問元組

print(a[2])

# 結果

>>> 3

字典建立字典 key -value

info =

print(type(info))

# 結果

# 訪問字典 通過建訪問值

print(info['name'])

# 結果

>>> 紀鑑航

# 訪問不存在的鍵

print(info['addr'])

# 結果(報錯)

>>> keyerror: 'addr'

# 當不存在這鍵的時候,可以返回預設設定的值,

# 有這個鍵就正常返回

print(info.get('addr', '撫順市'))

# 結果

>>> 撫順市

# 修改

info['age'] = 3

print(info)

# 結果

>>>

# 增加 當字典中不存在這個鍵, 就會新增

info['addr'] = '鞍山市'

print(info)

# 結果

>>>

# 刪除

del info['age']

print(info)

# 結果

>>>

# 遍歷

for k, v in info.items():

print(k, '---->', v)

# 結果

>>> name ----> 紀鑑航

gender ----> female

addr ----> 鞍山市

# 獲取所有鍵

print(list(info.keys()))

# 結果

>>> ['name', 'gender', 'addr']

# 獲取所有的值

print(list(info.values()))

# 結果

>>> ['紀鑑航', 'female', '鞍山市']

函式函式和方法的區別

函式 面向過程

方法 物件導向

python 中的函式

# 函式的定義

def say_hello(name):

print('hello', name)

say_hello('neusoft')

# 結果

>>> hello neusoft

# 1到 任意數之間累加和

def caculate_num(num):

sum_num = 0 # 存求和

for i in range(1, num+1):

sum_num = sum_num + i

return sum_num

print(caculate_num(100))

# 結果

>>> 5050

爬蟲爬蟲即網路爬蟲,英文是web spider。翻譯過來就是網路上爬行的蜘蛛,如果把網際網路看作一張大網,那麼爬蟲就是在大網上爬來爬去的蜘蛛,碰到想要的食物,就把他抓取出來。

我們在瀏覽器中輸入乙個**,敲擊回車,看到**的頁面資訊。這就是瀏覽器請求了**的伺服器,獲取到網路資源。那麼,爬蟲也相當於模擬瀏覽器傳送請求,獲得到html**。html**裡通常包含了標籤和文字資訊,我們就從中提取到我們想要的資訊。

通常爬蟲是從某個**的某個頁面開始,爬取這個頁面的內容,找到網頁中的其他鏈結位址,然後從這個位址爬到下乙個頁面,這樣一直不停的爬下去,進去批量的抓取資訊。那麼,我們可以看出網路爬蟲就是乙個不停爬取網頁抓取資訊的程式。

引用自

網頁爬取

# 匯入 *requests* 庫

import requests

# 獲取指定網域名稱的源**

response = requests.get('')

#響應的編碼方式

# 設定編碼方式

response.encoding = 'utf-8'

# 響應狀態碼 200 ok 404not found

print(response.status_code)

# 結果

>>> 200

print(response.encoding)

# 結果

>>> utf-8

# 獲取string型別相應

html_data = response.text

print(html_data)

# 將爬取的檔案寫成本地html

# 檔案路徑 讀寫模式 編碼方式

with open('index.html', 'w', encoding='utf-8') as f:

f.write(html_data)

爬取# 爬取

# 位址

url = ''

response2 = requests.get(url)

# 獲取byte型別的響應

img_data = response2.content

# 檔案路徑 讀寫模式write bingary 編碼方式

python爬蟲學習第二講

爬蟲網路請求模組 urllib.parse模組 解析url 聚焦網路爬蟲 根據既定目標有選擇的抓取某一特定主題內容 學習內容 增量式網路爬蟲 深層網路爬蟲 瀏覽器會對url進行編碼。除英文本元 數字和部分符號外,其餘全部使用百分號加十六進製制進行編碼,每個漢字由3個百分號的十六進製製碼組成 requ...

第二講案例

一 使用登入cookie方法 使用第一講案例中查詢響應檔案的方式找到該頁面的doc檔案,獲取請求頭中的cookie資訊 將該資訊以鍵值對的形式儲存到請求頭引數中即可,請使用自己的cookie值 headers 爬取完整程式如下,可參考 import requests def local 1 準備引數...

PYTHON第二講 輸入基本控制結構

print 基本控制結構for if python程式的塊都是用ident定義的 raw input 獲得使用者一行輸入,以回車結束返回乙個包含該行所有內容的字串 不包含回車 a raw input b split strip c int str d map function,list print ...