我想知道为什么当我调用 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)
我得到了所有请求的 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(错误)。
附加信息:有related question on SO ,其中提到了两种应对这种情况的方法:WAITING, header 。
等待在我的情况下不起作用
关于标题 - 我不知道在那里提供什么。
更新:具体版本,我正在尝试实现:
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)
您有需要去除的尾随换行符:
with open('dummyWords.txt') as fIn:
for word in map(str.strip, fIn) :
它适用于最后一个,因为您显然在文件中最后一个单词的末尾没有换行符。 "www.foo.com\n"
与 "www.foo.com"
不同
我是一名优秀的程序员,十分优秀!