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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

[python 学习] requests 库的使用

發布時間:2024/7/19 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [python 学习] requests 库的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、get請求

# -*- coding: utf-8 -*- import requestsURL_IP = "http://b.com/index.php" pyload = {'cate':1,'id':2} headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'} def use_simple_requests():r = requests.get(URL_IP, params=pyload, headers=headers)print r.url #resuest urlprint r.headers #response headerprint r.text #response 內容(編碼后) print r.content #response 內容(無編碼)print r.status_code#http狀態碼print r.json() #輸出json數據,服務器返回的不是json內容則會報錯#print r.raw.read(100) #獲取原始套接字相應,請求中需設置 stream=True use_simple_requests()

2、post請求

# -*- coding: utf-8 -*- import requestsURL_IP = "http://b.com/index.php" pyload = {'cate':1,'id':2} def use_simple_requests():r = requests.post('http://httpbin.org/post', data=pyload)print r.textuse_simple_requests()

3、cookie

  服務器端:http://a.com/index.php

<?php$cookie_name = $_COOKIE['name']; //接受客戶端發來的cookiesetcookie('name',$cookie_name.'777'); //將接受的cookie值追加'777'后發給客戶端 ?>

  客戶端:request_demo.py

# -*- coding: utf-8 -*- import requestsURL_IP = "http://b.com/index.php" cookies = {'name':'testtest'} #設置cookie def use_simple_requests():r = requests.get(URL_IP, cookies=cookies) #request請求中帶上cookieprint r.headersprint r.cookies['name'] #打印cookie use_simple_requests()

文檔:http://docs.python-requests.org/en/master/user/quickstart/#make-a-request

轉載于:https://www.cnblogs.com/natian-ws/p/7440859.html

總結

以上是生活随笔為你收集整理的[python 学习] requests 库的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。