Python请求模块,如何在for循环中发出多个请求? [英] Python requests module, how to issue multiple requests in the for loop?
问题描述
我想知道为什么当我依次调用 requests.get() 方法时,如下所示:
I wonder why when I call requests.get() method consequentially, like this:
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 循环中执行此操作时,例如:
I got OK status for all requests, but when I do it in the for loop, like:
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(错误).
I got 400(Error) for all requests except the last one.
附加信息:有关于SO的相关问题,其中提到了两种方式应对这种情况:等待,标题.
等待在我的情况下不起作用
以及关于标题 - 我不知道在那里提供什么.
Additional info:
there is related question on SO, where are mentioned 2 ways of coping with this situation: wait, headers.
wait doesn't work in my situation
and about headers - I don't know what to provide there.
更新:我正在尝试实施的特定版本:
Update: specific version, that I am trying to implement:
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)
推荐答案
您有需要去除的尾随换行符:
You have trailing newlines that you need to strip off:
with open('dummyWords.txt') as fIn:
for word in map(str.strip, fIn) :
它适用于最后一个,因为您显然在文件的最后一个单词的末尾没有换行符."www.foo.com
"
与 "www.foo.com"
It works for the last as you obviously have no newline at the end of the last word in the file. "www.foo.com
"
is not the same as "www.foo.com"
这篇关于Python请求模块,如何在for循环中发出多个请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!