1.response物件常用屬性
res.status_code:檢查請求是否成功
res.content:把response物件轉為二進位制資料
res.text:把response物件轉為字串資料
res.encoding:定義response物件的編碼
2.res.status_code 值的意義
1xx請求收到
2xx請求成功
3xx重寫向
4xx客戶端錯誤
5xx伺服器錯誤
3.robots協議
allow
disallo
在**後面加上/robots.txt即可看到協議
例子:
import requests
#引入requests庫
res1 = requests.
get(
(type
(res1)
(res1.status_code)
res = requests.
get(
'')#發出請求,並把返回的結果放在變數res中
pic=res.content
#把reponse物件的內容以二進位制資料的形式返回
)#新建了乙個檔案ppt.jpg,這裡的檔案沒加路徑,它會被儲存在程式執行的當前目錄下。
#內容需要以二進位制wb讀寫。
photo.
write
(pic)
#獲取pic的二進位制內容
photo.
close()
#關閉檔案
#引用requests庫
res3 = requests.
get(
'')novel=res3.text
#把response物件的內容以字串的形式返回
(novel[
:800])
#現在,可以列印**了,但考慮到整章太長,只輸出800字看看就好。
k =open
('c:\\users\\69505\desktop\\《三國演義》.txt'
,'a+'
)#建立乙個名為《三國演義》的txt文件,指標放在檔案末尾,追加內容
k.write
(novel)
#寫進檔案中
k.close
()
python語法基礎 8 包
官網解釋 packages are a way of structuring python s module namespace by using dotted module names 包是一種通過使用 模組名 來組織python模組命名空間的方式。具體的 包就是乙個包含有 init py檔案的資...
Python爬蟲基礎講解之請求
url又叫作統一資源定位符,是用於完整地描述internet上網頁和其他資源的位址的一種方法。類似於windows的檔案路徑。1.http 這個是協議,也就是http超文字傳輸協議,也就是網頁在網上傳輸的協議。2.mail 這個是伺服器名代表著是乙個郵箱伺服器,所以是mail。3.163.com 這...
Python學習筆記8 語法基礎
s i love hanxuexue print s i love hanxuexue 如果想表示出let s go來 1.可以使用巢狀引號,即外層使用雙引號 2.使用轉義字元 s let s go print s ss let s go print ss sss c user print sss ...