Python请求模块,如何在for循环中发出多个请求?
我想知道为什么我会这样调用requests.get()方法:Python请求模块,如何在for循环中发出多个请求?,python,python-requests,Python,Python Requests,我想知道为什么我会这样调用requests.get()方法: response = requests.get(url.format("set")) print(response.status_code) response = requests.get(url.format("map")) print(response.status_code) response = requests.get(url.format("list")) print(response.status_code) respo
response = requests.get(url.format("set"))
print(response.status_code)
response = requests.get(url.format("map"))
print(response.status_code)
response = requests.get(url.format("list"))
print(response.status_code)
response = requests.get(url.format("vector"))
print(response.status_code)
response = requests.get(url.format("string"))
print(response.status_code)
对于所有请求,我都获得了OK状态,但当我在for循环中执行此操作时,例如:
for word in fIn :
response = requests.get(url.format(word))
if(response.status_code == 200):
print "OK"
else:
print(response.status_code)
print "Error"
print word
除最后一个请求外,所有请求我都得到了400(错误)
其他信息:
这里提到了两种应对这种情况的方法:等待、标题。在我的情况下,等待不起作用
关于标题,我不知道该提供什么 更新: 我正在尝试实现的特定版本:
from lxml import html
import requests
fOut = open("descriptions.txt","w")
with open('dummyWords.txt') as fIn:
for word in fIn :
print word
response = requests.get(url.format(word))
if(response.status_code == 200):
print "OK"
else:
print(response.status_code)
print(word)
您需要去掉尾随的换行符:
它适用于最后一个单词,因为文件中最后一个单词的末尾显然没有换行符
“www.foo.com\n”
与“www.foo.com”
为我解决了编码POST数据的问题
cmd = urllib.quote(cmds[i])
我的测试用例
- 不需要换行
- 不需要睡眠时间
fIn
此处非常不清楚)。尝试显式地对word in('set'、'map'、'list'、'vector'、'string')执行:
作为开始。我认为标准是在最后一行的末尾应该有一个换行符。如果他不这样做,情况就不同了@蒂姆,你可以随心所欲地编辑文件,你不能做的是在URL上添加新行并期望它们正常工作。我的评论是指倒数第二句话——他应该有一个,但他显然没有。@Padraic Cunningham太棒了!!!我最初是在回家的路上想到的,这个想法不知怎么消失了!当然是这样!!!谢谢你细心的注视!祝你一天过得愉快!!致以最良好的祝愿@转八圈,别担心,你也是。
cmd = urllib.quote(cmds[i])