2

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)

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

I got 400(Error) for all requests except the last one.

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)
12
  • 3
    Do they have a rate limit on requests? Commented Jul 11, 2016 at 18:36
  • @Tim I am aware of rate limit, I don't know whether they have one. But it seems strange for me that typing requests manually works, while using for loop not. Maybe in the for loop every time new object for session is created and new session is established, while in manual requests one session is used? Commented Jul 11, 2016 at 18:39
  • Is this of any help? stackoverflow.com/questions/31306501/… Commented Jul 11, 2016 at 18:41
  • @spin_eight if you are typing them manually then perhaps you're doing one per half a second? The loop will do all 6 in 0.1 seconds, likely breaking a rate somewhere. Commented Jul 11, 2016 at 18:42
  • 1
    Looks like you are trying to get the input from a B.S. source like a file (fIn is very unclear here). Try explicitly doing for word in ('set', 'map', 'list', 'vector', 'string'): for a start. Commented Jul 11, 2016 at 18:46

2 Answers 2

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"

Sign up to request clarification or add additional context in comments.

5 Comments

I think standard is you should have a newline at the end of the last line. If he does or not, is different. stackoverflow.com/questions/729692/…
@Tim, you are free to edit a file as you please, what you cannot do is add newlines to urls and expect them to work.
My comment refers to the penultimate sentence - he should have one there, but he clearly doesn't.
@Padraic Cunningham great!!! I initially thought about it on my way home and this idea somehow vanished! Of course that is it!!! Thank you for you attentive look! Have a nice time of a day!! Best regards!
@spin_eight, no worries, you too.
0

Encoding POST data resolved issue for me.

cmd = urllib.quote(cmds[i])

For my testcases

  • No newline required
  • No sleep time required

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.