0

I am using python requests to connect to a website. I am passing some strings to get data about them.

The problem is some string contains slash /, so when they are passed in url, I got a ValueError.

this is my url:

https://api.flipkart.net/sellers/skus/%s/listings % string

when string is passed (string that does not contain slash), I get:

https://api.flipkart.net/sellers/skus/A35-Charry-228_39/listings 

It returns a valid response. but when i pass string which contains a slash:

string = "L20-ORG/BLUE-109(38)"

I get url like:

https://api.flipkart.net/sellers/skus/L20-ORG/BLUE-109(38)/listings

Which throws the error.

how to solve this?

1
  • what does your function look like? Commented Sep 25, 2015 at 9:19

2 Answers 2

3

Raw string literals in Python

string = r"L20-ORG/BLUE-109(38)"

You could find more info here and here.

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

2 Comments

I believe you may have misunderstood the question.
Not working. It still isn't skipping the slash in url
2

urllib.quote_plus is your friend. As urllib is a module from the standard library, you just have to import it with import urllib.

If you want to be conservative, just use it with default value:

string = urllib.quote_plus("L20-ORG/BLUE-109(38)")

gives 'L20-ORG%2FBLUE-109%2838%29'

If you know that some characters are harmless for your use case (say parentheses):

string = urllib.quote_plus("L20-ORG/BLUE-109(38)", '()')

gives 'L20-ORG%2FBLUE-109(38)'

12 Comments

giving urllib is not defined
@ManishGupta: urllib is a module from python standard library. Just add import urrlib before using it. (post edited accordingly)
I did that but it didn't work. Its just encoding them but in url it doesn't matter if you use encoded chars or slash
@ManishGupta: What is the error when the url is built with https://api.flipkart.net/sellers/skus/%s/listings % urllib.quote_plus(string)?
I used this string : "L20-ORG/BLUE-109(38)". The resulting url: https://api.flipkart.net/sellers/skus/L20-ORG%2FBLUE-109%2838%29/listings. And it threw ValueError. Its because url formed is wrong.
|

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.