标签: python-requests

带有 URL 路径变量的 Python http 删除请求

我有一个公开为 http://localhost:8080/test/api/v1/qc/{id} 的 Http 端点用于删除,在进行此 API 删除调用时,我必须用正确的 id 替换

我使用 python 的 requests 模块尝试了以下方法

param = {
  "id" : 1
}

requests.delete(url = http://localhost:8080/test/api/v1/qc/{id}, params=param)
Run Code Online (Sandbox Code Playgroud)

此 API 调用因错误而中断

ValueError: No JSON object could be decoded.
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

python path-variables python-requests

0
推荐指数
1
解决办法
3275
查看次数

如何从 python 的输出中删除大括号?

我是 Python 新手,我最近 1 周前开始学习,但我被困在这里......任何帮助将不胜感激......

from tkinter import *
import tkinter as tk
import psycopg2

root = Tk()

def get_info(Employee_Name, Department, Education,Salary):
   con = psycopg2.connect(dbname = 'postgres',user = 'postgres',password = 'Gaurav@31',host = 'localhost',port = 5432)
   cur = con.cursor()
   query = ('''INSERT INTO Employee (Employee_name, Department, Education,Salary) VALUES (%s,%s,%s,%s);''' )
   cur.execute(query,(Employee_Name, Department, Education,Salary))
   print('Data inserted Successfully')
   con.commit()
   con.close()
   display_all()

def search(emp_id):
    con = psycopg2.connect(dbname = 'postgres',user = 'postgres',password = 'Gaurav@31',host = 'localhost',port = 5432)
    cur = con.cursor()
    query = ('''SELECT * …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-3.x python-requests

0
推荐指数
1
解决办法
1891
查看次数

创建一个纯Python API(没有任何框架),邮递员客户端可以成功发布json请求

以下是我尝试过的。

import http.server
import socketserver
import requests

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

def api(data):
    r = requests.post('http://localhost:8000/api', json=data)
    return r.json()
Run Code Online (Sandbox Code Playgroud)

使用上面的代码出现以下错误。

ConnectionRefusedError: [WinError 10061] 由于目标计算机主动拒绝而无法建立连接

Postman 应该能够发送具有 json 正文的 post 请求。

python api http python-requests

0
推荐指数
1
解决办法
5344
查看次数

使用 Loguru 日志库记录导入文件中发出的请求

因此,我在 python 文件(main.py)中编写了程序,该文件使用 api 包装文件(bluerev.py)中的类。我想使用 main.py 中的 loguru 记录器来收集程序中的所有异常 + api 包装器中发出的所有请求。因此,bluerev.py api 包装器中设置的日志记录如下所示:

import logging

#Logging setup
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

class BluerevApiRequestHandler:

def __init__(self):
    self.date_str_format = "%Y-%m-%dT%H:%M:%S.%f"

@staticmethod
def setup_logger_for_requests():
    """
    Sets up the requests library to log api requests
    """
    logger.setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True
Run Code Online (Sandbox Code Playgroud)

main.py 日志记录代码如下所示:

from blurev import *
from loguru import logger
#more imports and code

@logger.catch
def main():
    # associated file and credential locations
    gmail_creds_file = "gmail_creds.json"
    revu_creds_file = r"revu_credentials.json" …
Run Code Online (Sandbox Code Playgroud)

python logging python-3.x python-requests loguru

0
推荐指数
1
解决办法
5415
查看次数

无法从包含非常规内容的网页中获取某些字段

当我尝试使用请求从网页中抓取某些字段时,我遇到了如下所示的外部内容,我找不到任何从中获取数据的想法。以下是我收到的回复的一小部分:

    I: Qc[
    j~O~_
]S6gMWNougj~Ougp_{
    hr[
        ogL>i
    ]OqNotnYr=nQHOGirKugLKngL[
        Kh
    ][
        uiMpnTnNpSYR: QIZSXNxNpOq_MRnTnN5Nn|nV[
            _SNotnOIh~Rn|9 RIFnQHOuh6Smg\[
                S^\qxf\: sY8OKjL[
                    yhMVnTnO__
                ]RnQHOYhZ_5NotnRn|
            }TYFnQHOIf
        ]W: NotnVZSZY59nQHOuh5
    }uh8Wq_J_{
        hqSmgLZnTnOTg~NxNpOmiLmNotnRnNxNp>8 gr[
            ~NotnW5>
        ]XZ: MQJ6OV5mGWZ|lXnNxNrqz[
            LK9WL[
                xf\:
            }i\[
Run Code Online (Sandbox Code Playgroud)

我想知道是否有任何方法可以将内容转换为常规 html 或 json。

这是这些元素中应该包含的一些地址:

Franklin St
Great Rd
Nonset Path
Run Code Online (Sandbox Code Playgroud)

python web-scraping python-3.x python-requests

0
推荐指数
1
解决办法
460
查看次数

如何判断 requests.Session() 是否良好?

我有以下与会话相关的代码,必须连续运行。

代码

import requests
http = requests.Session()

while True:
    # if http is not good, then run http = requests.Session() again
    response = http.get(....)
    # process respons
    # wait for 5 seconds
Run Code Online (Sandbox Code Playgroud)

注意:我将线路移出http = requests.Session()了循环。

问题

如何检查会话是否正常工作

不工作会话的一个示例可能是在 Web 服务器重新启动之后。或者负载均衡器重定向到不同的 Web 服务器。

python session python-requests

0
推荐指数
1
解决办法
5625
查看次数

生成图表的数据请求始终为空

我正在尝试使用 python 的请求模块来抓取在网站上生成图表的数据。

我的代码目前如下所示:

# load modules
import os
import json
import requests as r

# url to send the call to
postURL = <insert website>

# utiliz get to pull cookie data
cookie_intel = r.get(postURL, verify = False)

# get cookies
search_cookies = cookie_intel.cookies

#### Request Information ####

# API request data
post_data = <insert request json>

# header information
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"}

# results 
results_post = r.post(postURL, …
Run Code Online (Sandbox Code Playgroud)

python web-scraping python-requests

0
推荐指数
1
解决办法
80
查看次数

如何使用python登录网站

可能重复:
如何使用Python的Requests模块"登录"网站?

我正在使用python 3.2.3编写应用程序,我不知道如何登录网站.我需要登录...并保存cookie(我认为),但我尝试的一切都不起作用.

我曾尝试过请求,但没有运气.我的网页在localhost上运行(它只是简单的登录表单).

FORM:
    <form method="post" action="">
            <input type="text" name="uname" placeholder="Uporabnisko ime:"/>
            <input type="password" name="password" placeholder="Geslo:"/>
            <input type="submit" value="Send"/>
    </form>
Run Code Online (Sandbox Code Playgroud)

蟒蛇:

import requests
payload = {'uname': 'name', 'password': 'pswd123'}
r = requests.post('http://localhost/python', data=payload)
Run Code Online (Sandbox Code Playgroud)

这什么都不做.如何发布姓名和用户名..感谢您的回复.

python post login python-3.x python-requests

-1
推荐指数
1
解决办法
1万
查看次数

如何从python中的url请求将unicode文件(jpg)写入另一个文件

我试图.jpg使用requestspython中的模块从URL 下载文件.这是我试过的.没有错误.但是我无法打开输出文件.

>>> import requests
>>> l = requests.get("http://www.mosta2bal.com/vb/imgcache/2/9086screen.jpg")
>>> l
<Response [200]>
>>> l.text
u'\ufffd\ufffd\ufffd\ufffd\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\ufffd\ufffd\x12EExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x......long text
>>> l.encoding
>>> import codecs
>>> f = codecs.open('out.jpg', mode="w", encoding="utf-8")
>>> f.write(l.text)
Run Code Online (Sandbox Code Playgroud)

python unicode file-io python-requests

-1
推荐指数
1
解决办法
961
查看次数

Does Python Requests POST need numerical data to be a string rather than a float?

Here is a small quote from this answer:

import requests
import json

data = {"data" : "24.3"}
data_json = json.dumps(data)
headers = {'Content-type': 'application/json'}

response = requests.post(url, data=data_json, headers=headers)
Run Code Online (Sandbox Code Playgroud)

Does anyone know for sure whether it matters whether you have

data = {"data" : "24.3"}

or

data = {"data" : 24.3} ?

python python-requests

-1
推荐指数
1
解决办法
483
查看次数