日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python 爬虫利器一之 Requests 库的用法

發(fā)布時間:2025/3/15 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 爬虫利器一之 Requests 库的用法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡介

  Requests是用python語言基于urllib編寫的,采用的是Apache2 Licensed開源協(xié)議的HTTP庫,Requests它會比urllib更加方便,可以節(jié)約我們大量的工作。

一、安裝

?pip快速安裝:pip install requests

二、使用

import requestsresponse = requests.get("https://www.baidu.com") print(type(response)) print(response.status_code) print(type(response.text))response.enconding = "utf-8' print(response.text)print(response.cookies)print(response.content) print(response.content.decode("utf-8"))

response.text返回的是Unicode格式,通常需要轉(zhuǎn)換為utf-8格式,否則就是亂碼。response.content是二進制模式,可以下載視頻之類的,如果想看的話需要decode成utf-8格式。
  不管是通過response.content.decode("utf-8)的方式還是通過response.encoding="utf-8"的方式都可以避免亂碼的問題發(fā)生

2、一大推請求方式

import requests requests.post("http://httpbin.org/post") requests.put("http://httpbin.org/put") requests.delete("http://httpbin.org/delete") requests.head("http://httpbin.org/get") requests.options("http://httpbin.org/get")

基本GET:

import requestsurl = 'https://www.baidu.com/' response = requests.get(url) print(response.text)

r.encoding ? ? ? ? ? ? ? ? ? ? ? #獲取當(dāng)前的編碼
r.encoding = 'utf-8' ? ? ? ? ? ? #設(shè)置編碼
r.text ? ? ? ? ? ? ? ? ? ? ? ? ? #以encoding解析返回內(nèi)容。字符串方式的響應(yīng)體,會自動根據(jù)響應(yīng)頭部的字符編碼進行解碼。
r.content ? ? ? ? ? ? ? ? ? ? ? ?#以字節(jié)形式(二進制)返回。字節(jié)方式的響應(yīng)體,會自動為你解碼 gzip 和 deflate 壓縮。

r.headers ? ? ? ? ? ? ? ? ? ? ? ?#以字典對象存儲服務(wù)器響應(yīng)頭,但是這個字典比較特殊,字典鍵不區(qū)分大小寫,若鍵不存在則返回None

r.status_code ? ? ? ? ? ? ? ? ? ? #響應(yīng)狀態(tài)碼
r.raw ? ? ? ? ? ? ? ? ? ? ? ? ? ? #返回原始響應(yīng)體,也就是 urllib 的 response 對象,使用 r.raw.read() ??
r.ok ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# 查看r.ok的布爾值便可以知道是否登陸成功
?#*特殊方法*#
r.json() ? ? ? ? ? ? ? ? ? ? ? ? #Requests中內(nèi)置的JSON解碼器,以json形式返回,前提返回的內(nèi)容確保是json格式的,不然解析出錯會拋異常
r.raise_for_status() ? ? ? ? ? ? #失敗請求(非200響應(yīng))拋出異常

帶參數(shù)的GET請求:

  如果想查詢http://httpbin.org/get頁面的具體參數(shù),需要在url里面加上,例如我想看有沒有Host=httpbin.org這條數(shù)據(jù),url形式應(yīng)該是http://httpbin.org/get?Host=httpbin.org

  下面提交的數(shù)據(jù)是往這個地址傳送data里面的數(shù)據(jù)。

import requestsurl = 'http://httpbin.org/get' data = {'name':'zhangsan','age':'25' } response = requests.get(url,params=data) print(response.url) print(response.text)#URL傳遞參數(shù) payload = {'keyword': '香港', 'salecityid': '2'} r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) print(r.url) #示例為http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=香港

POST請求

# 1、基本POST實例import requestspayload = {'key1': 'value1', 'key2': 'value2'} ret = requests.post("http://httpbin.org/post", data=payload)print(ret.text)# 2、發(fā)送請求頭和數(shù)據(jù)實例import requests import jsonurl = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} headers = {'content-type': 'application/json'}ret = requests.post(url, data=json.dumps(payload), headers=headers)print(ret.text) print(ret.cookies)

?

https://cuiqingcai.com/2556.html

https://www.cnblogs.com/lei0213/p/6957508.html

https://www.cnblogs.com/lei0213/p/6957508.html

總結(jié)

以上是生活随笔為你收集整理的Python 爬虫利器一之 Requests 库的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。