标签: python-requests

在 Python 中的 requests.post() 中发送变量作为数据参数

我试图将变量传递到 requests.post() 中的数据字段,但我继续收到错误,

  Error Response: {'error': {'message': 'Exception while reading request', 
'detail': 'Cannod decode: java.io.StringReader@1659711'}, 'status': 'failure'}
Run Code Online (Sandbox Code Playgroud)

这是我的代码

#Fill array from CSV
temp=[]
for row in csv.iterrows():
    index, data = row
    temp.append(data.tolist())


#Create new asset for all assets in CSV
for index, row in enumerate(temp):
    make = temp[index][0]
    serial = str(temp[index][1])
    date = str(temp[index][2])
    response = requests.post(url, auth=(user, pwd), headers=headers, 
    data='{"asset_tag":"test1", "assigned_to":"test2", 
    "company":"test3", "serial_number":serial}')
Run Code Online (Sandbox Code Playgroud)

我最初尝试使用直接从 CSV 提供数据

str(temp[index][1])
Run Code Online (Sandbox Code Playgroud)

这不起作用,所以我尝试分配str(temp[index][1])给变量serial,然后像这样传递变量,但这也会导致相同的错误。

如果能找到正确的方向就太好了,谢谢!

python post python-requests servicenow servicenow-rest-api

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

Python 请求:如何处理状态码 304

我正在尝试使用requestsbs4从网站获取信息,但收到状态代码 304 并且没有来自request.get(). 我已经阅读并了解此代码表明资源已在我的缓存中。我如何从我的缓存访问资源,或者最好清除我的缓存以便我可以接收新资源?

我试过添加以下标题:headers={'Cache-Control': 'no-cache'}requests.get()但仍然有同样的问题。

此外,我已经研究了该requests-cache模块,但不清楚如何或是否可以使用它来解决问题。

代码:

import requests

r = requests.get('https://smsreceivefree.com/')

print(r.status_code)
print(r.content)
Run Code Online (Sandbox Code Playgroud)

输出:

304
b''
Run Code Online (Sandbox Code Playgroud)

python python-requests

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

如何使用请求跟踪页面重定向

我有这个简单的代码:

import requests
r = requests.get('https://yahoo.com')
print(r.url)
Run Code Online (Sandbox Code Playgroud)

执行后打印:

https://uk.yahoo.com/?p=us
Run Code Online (Sandbox Code Playgroud)

我想看看:

  1. 在到达之前发生了多少次重定向https://uk.yahoo.com/?p=us(显然,我最初输入时有重定向https://yahoo.com)?

  2. 我还想保存每一页的内容,而不仅仅是最后一页。这个怎么做?

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

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

恐慌:接口转换:interface {} 是字符串,而不是 float64

我正在尝试将这个简单的 python 函数转换为 golang,但面临此错误的问题

panic: interface conversion: interface {} is string, not float64
Run Code Online (Sandbox Code Playgroud)

python

def binance(crypto: str, currency: str):
    import requests

    base_url = "https://www.binance.com/api/v3/avgPrice?symbol={}{}"
    base_url = base_url.format(crypto, currency)
  
    try:
        result = requests.get(base_url).json()
        print(result)
        result = result.get("price")
    except Exception as e:
        
        return False
    return result
Run Code Online (Sandbox Code Playgroud)

这是 golang 版本(比应有的代码更长、更复杂)

func Binance(crypto string, currency string) (float64, error) {
    req, err := http.NewRequest("GET", fmt.Sprintf("https://www.binance.com/api/v3/avgPrice?symbol=%s%s", crypto, currency), nil)
    if err != nil {
         fmt.Println("Error is req: ", err)
    }
    
    client := &http.Client{}
    
    resp, err := …
Run Code Online (Sandbox Code Playgroud)

go python-requests type-assertion

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

如何使用 Python 发布很长的 URL - 请求模块

URL 类似于下面的内容并且很长,可能会延伸到 1 页以上。

http://example.com:8080/testAPI/testAPIServlet?id=123|345 ..................................... ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ...................................|789&rows=200

使用 requests.get 方法不支持这种类型的 url。所以我想使用 requests.post 方法来获取响应。请帮忙。

python python-requests

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

我怎样才能加快速度呢?(urllib2,要求)

问题:我正在尝试验证验证码可以是0000-9999中的任何内容,使用正常的请求模块需要大约45分钟来完成所有这些(0000-9999).我怎样才能多线程或加速它?如果我能从站点获取HTTP状态代码以查看我是否成功获得了正确的代码或者它是否正确(200 =正确,400 =不正确)如果我可以获得两个示例(GET和POST),那将非常有用这太棒了!

我一直在搜索,我看的大多数模块已经过时了(我最近一直在使用grequests)

example url = https://www.google.com/
example params = captcha=0001
example post data = {"captcha":0001}
Run Code Online (Sandbox Code Playgroud)

谢谢!

python urllib2 python-requests

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

使用 beautiful soup 抓取 twitter 时出现问题

使用 beautiful soup 和 requests 库抓取 Facebook 或 Twitter 等带有大量 html 标签的大型网站时出现问题。

from bs4 import BeautifulSoup 
import requests 

html_text = requests.get('https://twitter.com/elonmusk').text

soup = BeautifulSoup(html_text, 'lxml')
elon_tweet = soup.find_all('span', class_='css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0')
print(elon_tweet)
Run Code Online (Sandbox Code Playgroud)

推文及其相应的跨度

全跨度图像

img 链接到跨度

当代码执行时,它返回一个空列表。

我是网络抓取的新手,欢迎详细解释。

python beautifulsoup python-requests

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

无法使用请求模块从静态网页中抓取不同的公司名称

我创建了一个脚本来使用请求模块从该网站收集不同的公司名称,但是当我执行该脚本时,它最终什么也没得到。我在页面源中查找了公司名称,发现这些名称在那里可用,因此它们似乎是静态的。

import requests
from bs4 import BeautifulSoup

link = 'https://clutch.co/agencies/digital-marketing'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}

with requests.Session() as s:
    s.headers.update(headers)
    res = s.get(link)
    soup = BeautifulSoup(res.text,"lxml")
    for item in soup.select("h3.company_info > a"):
        print(item.text)
Run Code Online (Sandbox Code Playgroud)

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

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

无法使用请求模块从网页中抓取列表链接

Oxford, Oxfordshire我正在尝试使用请求模块从此网页中抓取此搜索的不同列表。这是我点击搜索按钮之前输入框的样子。

我已经定义了一个准确的选择器来定位列表,但脚本无法获取任何数据。

import requests
from pprint import pprint
from bs4 import BeautifulSoup

link = 'https://www.zoopla.co.uk/search/'

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.9,bn;q=0.8',
    'Referer': 'https://www.zoopla.co.uk/for-sale/',
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
}
params = {
    'view_type': 'list',
    'section': 'for-sale',
    'q': 'Oxford, Oxfordshire',
    'geo_autocomplete_identifier': 'oxford',
    'search_source': 'home'
}
res = requests.get(link,params=params,headers=headers)
soup = BeautifulSoup(res.text,"html5lib")
for item in soup.select("[id^='listing'] a[href^='/for-sale/details/']:has(h2[data-testid='listing-title'])"):
    print(item.get("href"))
Run Code Online (Sandbox Code Playgroud)

编辑:

如果我尝试类似以下的操作,脚本似乎可以完美运行。唯一的主要问题是我必须在标头中使用硬编码的 cookie,这些 cookie …

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

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

python请求带有标头和参数的POST

我有一个发帖请求,试图requests在python中发送。但是我收到无效的403错误。请求可以通过浏览器正常运行。

POST /ajax-load-system HTTP/1.1
Host: xyz.website.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-GB,en;q=0.5
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Referer: http://xyz.website.com/help-me/ZYc5Yn
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 56
Cookie: csrf_cookie_name=a3f8adecbf11e29c006d9817be96e8d4; ci_session=ba92hlh6o0ns7f20t4bsgjt0uqfdmdtl; _ga=GA1.2.1535910352.1530452604; _gid=GA1.2.1416631165.1530452604; _gat_gtag_UA_21820217_30=1
Connection: close

csrf_test_name=a3f8adecbf11e29c006d9817be96e8d4&vID=9999
Run Code Online (Sandbox Code Playgroud)

我在python中尝试的是:

import requests
import json

url = 'http://xyz.website.com/ajax-load-system'

payload = {
'Host': 'xyz.website.com',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-GB,en;q=0.5',
'Referer': 'http://xyz.website.com/help-me/ZYc5Yn',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Content-Length': '56', …
Run Code Online (Sandbox Code Playgroud)

python post request python-3.x python-requests

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

如何在请求中执行DELETE,PUT

我可以使用以下命令进行POST或GET请求:

requests.get(...)
requests.post(...)
Run Code Online (Sandbox Code Playgroud)

我怎么会做request.PUTrequest.DELETE

python python-requests

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

无法与 API 通信

我正在尝试抓取以下网站

我注意到在页面之间导航期间对以下端点执行了 XHR POST 请求,如您在以下打印屏幕中所见:

在此处输入图片说明

在 POST 请求中卡住的东西,我注意到后面有一个动态值,GBK-但我不明白它是从哪里生成的或如何获取它的。

如果您只是在页面之间导航,您会注意到值保持变化。

根据以下回答更新Life is complex

这是POST向 API发送请求的方法:

import requests

# we need the value!
url = "http://app1.nmpa.gov.cn/data_nmpa/face3/search.jsp?6SQk6G2z=GBK-Value"

# here you can add headers as you need!
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0",
    "Accept": "*/*",
    "Accept-Language": "en-US,en;q=0.5",
    "cache-control": "no-cache",
    "Content-Type": "application/x-www-form-urlencoded",
    "Pragma": "no-cache"
}

data = {
    "tableId": "27",
    "State": [
        "1",
        "1",
        "1",
        "1",
        "1",
        "1",
        "1" …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup python-requests

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

网页抓取程序找不到我可以在浏览器中看到的元素

我正在尝试使用 Requests 和 BeautifulSoup在https://www.twitch.tv/directory/game/Dota%202上获取流的标题。我知道我的搜索条件是正确的,但我的程序没有找到我需要的元素。

这是一个屏幕截图,显示了浏览器中源代码的相关部分:

显示部分网页的浏览器屏幕截图,以及使用开发人员工具/网络检查器的源代码。

作为文本的 HTML 源代码:

<div class="tw-media-card-meta__title">
  <div class="tw-c-text-alt">
    <a class="tw-full-width tw-interactive tw-link tw-link--button tw-link--hover-underline-none tw-link--inherit" data-a-target="preview-card-title-link" href="/weplayesport_en">
      <div class="tw-align-items-start tw-flex">
        <h3 class="tw-ellipsis tw-font-size-5" title="NAVI vs HellRaisers | BO5 | ODPixel &amp; S4 | WeSave! Charity Play">NAVI vs HellRaisers | BO5 | ODPixel &amp; S4 | WeSave! Charity Play</h3>
      </div>
    </a>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import requests
from bs4 import BeautifulSoup

req = requests.get("https://www.twitch.tv/directory/game/Dota%202")

soup = BeautifulSoup(req.content, "lxml")

title_elems = soup.find_all("h3", attrs={"title": True})

print(title_elems)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,title_elems …

python beautifulsoup web-scraping python-requests

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