我想知道为什么当我按照这样调用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)
response = requests.get(url.format("vector"))
print(response.status_code)
response = requests.get(url.format("string"))
print(response.status_code)
我获得了所有请求的正常状态,但是当我在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(错误)。
其他信息: 有related question on SO,其中提到了两种应对这种情况的方法:等待,标题 在我的情况下等待不起作用 关于标题 - 我不知道在那里提供什么。
更新: 特定版本,我正在尝试实施:
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)
答案 0 :(得分:3)
You have trailing newlines that you need to strip off:
with open('dummyWords.txt') as fIn:
for word in map(str.strip, fIn) :
It works for the last as you obviously have no newline at the end of the last word in the file. "www.foo.com\n"
is not the same as "www.foo.com"
答案 1 :(得分:0)
为我解决POST数据编码问题。
cmd = urllib.quote(cmds[i])
对于我的测试用例