搜索
首页后端开发Python教程python requests post如何使用

python requests post如何使用

Apr 29, 2023 pm 04:52 PM
pythonpostrequests

    python模拟浏览器发送post请求

    import requests

    格式request.post

    request.post(url, data, json, kwargs) # post请求格式
    request.get(url, params, kwargs) # 对比get请求

    发送post请求 传参分为

    • 表单(x-www-form-urlencoded)

    • json(application/json)

    data参数支持字典格式和字符串格式,字典格式用json.dumps()方法把data转换为合法的json格式字符串 次方法需要导入json模块;

    import json
    json.dumps(data) # data转换成json格式

    或者将data参数赋值给post方法的json参数,必须为合法json格式,否则没用,如果有布尔值要小写,不能有非Unicode字符。

    表单方式的post请求(x-www-form-urlencoded)

    import requests
    url = "https://editor.net/"
    data = {"key": "value"} # 字典 外层无引号
    resp = requests.post(url,data=data)
    print(resp.text)

    json类型的post请求

    import requests
    url = "https://editor.net/"
    data = '{"key": "value"}' # 字符串格式 
    resp = requests.post(url, data=data)
    print(resp.text)

    使用字典格式填写参数,传递时转换为json格式

    (1)json.dumps()方法转换

    import requests
    import json
    url = "https://editor.net/"
    data = {"key": "value"}
    resp = requests.post(url, data=json.dumps(data))
    print(resp.text)

    (2)将字典格式的data数据赋给post方法的json参数

    import requests
    import json
    url = "https://editor.net/"
    data = {"key": "value"}
    resp = requests.post(url, json=data)
    print(resp.text)

    python requests post数据的几个问题的解决

    最近在用Requests做一个自动发送数据的小程序,使用的是Requests库,在使用过程中,对于post数据的编码有一些问题,查找很多资料,终于解决。

    post数据的urlencode问题

    我们一般post一个dict数据的时候,requests都会把这个dict里的数据进行urlencode,再进行发送。

    但我发现他用的urlencode默认是UTF-8编码,如果我的网站程序只支持gb2312的urlencode怎么办呢?

    可以引入urllib中的urllib.parse.urlencode进行编码。

    from urllib.parse import urlencode
    import requests
     
    session.post('http://www.bac-domm.com',   data=urlencode({'val':'中国人民'}, encoding='gb2312'),  headers = head_content)

    避免数据被urlencode的问题

    有时我们并不希望数据进行urlencode,怎么办?

    只要在post的data里拼接成字符串就可以了,当然在拼接的时候要注意字符串的编码问题,比如说含有中文时,就应该把编码设置为utf-8或gb2312

    vld = 'val:中国人民'
    session.post('http://www.bac-domm.com',   data=vld.encode('utf-8'),  headers = head_content)

    以上是python requests post如何使用的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明
    本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
    在Python阵列上可以执行哪些常见操作?在Python阵列上可以执行哪些常见操作?Apr 26, 2025 am 12:22 AM

    Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

    在哪些类型的应用程序中,Numpy数组常用?在哪些类型的应用程序中,Numpy数组常用?Apr 26, 2025 am 12:13 AM

    NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

    您什么时候选择在Python中的列表上使用数组?您什么时候选择在Python中的列表上使用数组?Apr 26, 2025 am 12:12 AM

    useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

    所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?所有列表操作是否由数组支持,反之亦然?为什么或为什么不呢?Apr 26, 2025 am 12:05 AM

    不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

    您如何在python列表中访问元素?您如何在python列表中访问元素?Apr 26, 2025 am 12:03 AM

    toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

    Python的科学计算中如何使用阵列?Python的科学计算中如何使用阵列?Apr 25, 2025 am 12:28 AM

    Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

    您如何处理同一系统上的不同Python版本?您如何处理同一系统上的不同Python版本?Apr 25, 2025 am 12:24 AM

    你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

    与标准Python阵列相比,使用Numpy数组的一些优点是什么?与标准Python阵列相比,使用Numpy数组的一些优点是什么?Apr 25, 2025 am 12:21 AM

    numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

    See all articles

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    Video Face Swap

    Video Face Swap

    使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

    热工具

    mPDF

    mPDF

    mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

    SecLists

    SecLists

    SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

    VSCode Windows 64位 下载

    VSCode Windows 64位 下载

    微软推出的免费、功能强大的一款IDE编辑器

    SublimeText3汉化版

    SublimeText3汉化版

    中文版,非常好用

    WebStorm Mac版

    WebStorm Mac版

    好用的JavaScript开发工具