首页 > 解决方案 > 使用 requests.post 遍历多个 url

问题描述

例如,我有一个这样的列表(我有 100 个网址):

https://google.com
https://facebook.com
https://yahoo.com
https://stackoverflow.com

保存在cisco.txt 我在 python 中制作了这个脚本,在每个 url 上发出一个 post 请求并给我响应,但它只请求一个 url,所以我想遍历每个 url

import requests
import ssl
import sys
with open('cisco.txt') as fp:
   for line in fp:
      print(line)
   request =(line) 
    
   data={'SAMLResponse':'test'}
   response = requests.post(request, data, verify=False)
    
   print(response.content)

标签: pythonpython-3.xpython-requests

解决方案


您应该在循环中完成所有操作。目前,最后一行将设置请求,然后您才开始提出一个请求。

import requests
import ssl
import sys

with open('cisco.txt') as fp:
    for line in fp:
        print(line)
        request =(line) 
        # keep all of this in the loop
        data={'SAMLResponse':'test'}
        response = requests.post(request, data, verify=False)
        print(response.content)

推荐阅读