首页 > 解决方案 > 为什么 requests.post() 不在 python 中使用提供的代理?

问题描述

我正在尝试发送带有 http 代理列表的发布请求。有一段时间我认为代理工作正常,直到我在列表中添加一个假代理以确保一切都像这样工作:


def func_name(i):
  url = 'https://www.some-url.com/Cart/ajax/page.php'

  # Proxies to connect with
  proxies_list = [
    'http://1.1.1.1:2000',
    'http://2.2.2.2:2000',
    'http://1.2.3.0:2000'       # This is the fake one
  ]
  proxy_index = random.randint(0, len(proxies_list) - 1)
  proxy = {"http": proxies_list[proxy_index]}

  # List of user agents
  headers_list = [
    'Linux Mozilla 5/0',
    'Linux Mozilla 5/0',
    'Linux Mozilla 5/0'
  ]
  headers_index = random.randint(0, len(headers_list) - 1)
  headers = {'user-agent':headers_list[headers_index], 'Accept-Encoding':'none'}

  payload = {'dataToValidate':str(i),'actionName':'nc_signup'}
  answer = requests.post(url=url, headers=headers, proxies=proxy, data=payload).json()
  print(answer)

When proxy_index = random.randint(0, len(proxies_list) - 1)picks the fake one I get an answer anyways, the reason for that may be because the requests.post()function doesn't even use the argument proxies=proxyas expected.

标签: pythonhttpproxy

解决方案


由于您仅proxy使用选项构造对象http,因此您尝试加载的 HTTPS URL 不会被代理。改变

proxy = {"http": proxies_list[proxy_index]}

proxy = {"http": proxies_list[proxy_index], "https": proxies_list[proxy_index]}

应该解决问题。


推荐阅读