4

I'm trying to convert a YQL string with spaces to html code.

Here's the YQL string I'm using:

select * from yahoo.finance.historicaldata where symbol in ("MSFT", "AAPL") and startDate = "2010-10-01" and endDate = "2010-10-05"

Here's the url code that needs to come out:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22MSFT%22%2C%20%22AAPL%22)%20and%20startDate%20%3D%20%222010-10-01%22%20and%20endDate%20%3D%20%222010-10-05%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

I've been trying to do this with urllib.parse.urlencode but have not had any luck.

I can get up to "http://query.yahooapis.com/v1/public/yql?" but afterwards it starts to break down. Is there a pythonesque way to do this that I'm just missing?

Thank you!

0

2 Answers 2

2
import urllib

url_start = "http://query.yahooapis.com/v1/public/yql?q="
url_end = "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="

you can encode the string as a dict like this:

d = {'select * from yahoo.finance.historicaldata where symbol in ("MSFT", "AAPL") and startDate':'"2010-10-01"', 'and endDate':'"2010-10-05"'}

encode_url = urllib.urlencode(d)

#output is:
'select+%2A+from+yahoo.finance.historicaldata+where+symbol+in+%28%22MSFT%22%2C+%22AAPL%22%29+and+startDate=%222010-10-01%22&and+endDate=%222010-10-05%22'

url = url_start + encode_url + url_end
Sign up to request clarification or add additional context in comments.

4 Comments

urllib is a package with nothing in it in Python 3.x. You probably want urllib.parse.
I don't think this works. I tried combining your three strings and the page gives an error. The URL I provide gives actual data.
I think abarnet is right. It seems urllib was split into parts for python3.x, and I wasn't aware. However, if urllib.parse has a urlencode method, that should do the same job, if applied like above.. quote_plus method also might work with urllib.parse, I would try that.
As I mentioned above. If I copy your three string outputs into one large string, that still doesn't work, so I don't think it's a urllib.parse issue.
2
>>> import urllib
>>> urllib.quote('http://www.example.com/déjà-vu/example two.html')
'http%3A//www.example.com/d%C3%A9j%C3%A0-vu/example%20two.html'

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.