from googlefinance import getQuotes
print(getQuotes("NSE:M\&MFIN"),)
The ampersand is being treated as code but I want to treat it as text; I get a bad request exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/.../site-packages/googlefinance/__init__.py", line 70, in getQuotes
content = json.loads(request(symbols))
File "/.../site-packages/googlefinance/__init__.py", line 33, in request
resp = urlopen(req)
File "/.../urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/.../urllib/request.py", line 532, in open
response = meth(req, response)
File "/.../urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/.../urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/.../urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/.../urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
I tried to escape it (using a \) but this did not work either.
How can I treat this ampersand as text?
The library is rather naive in that it just appends the symbols to a URL without proper encoding, see the source code:
def buildUrl(symbols):
symbol_list = ','.join([symbol for symbol in symbols])
# a deprecated but still active & correct api
return 'http://finance.google.com/finance/info?client=ig&q=' \
+ symbol_list
You can work around this by manually quoting up front, using the urllib.parse.quote() function:
from urllib.parse import quote
print(getQuotes(quote("NSE:M&MFIN")))
Demo:
>>> from googlefinance import getQuotes
>>> from urllib.parse import quote
>>> print(getQuotes(quote("NSE:M&MFIN")))
[{'ID': '11784956', 'StockSymbol': 'M&MFIN', 'Index': 'NSE', 'LastTradePrice': '416.55', 'LastTradeWithCurrency': '₹416.55', 'LastTradeTime': '3:30PM GMT+5:30', 'LastTradeDateTime': '2017-08-18T15:30:00Z', 'LastTradeDateTimeLong': 'Aug 18, 3:30PM GMT+5:30'}]
Related
So I have been trying out coding and am currently finding some language detection packages and found out about textblob, but I am having some sort of proble.
This is my code:
# - *- coding: utf- 8 - *-
from textblob import TextBlob
blob = TextBlob("Comment vas-tu?")
print(blob.detect_language())
print(blob.translate(to='es'))
print(blob.translate(to='en'))
print(blob.translate(to='zh'))
and this error shows:
Traceback (most recent call last):
File "C:\Users\*****\PycharmProjects\pythonProject\main.py", line 6, in <module>
print(blob.detect_language())
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\site-packages\textblob\blob.py", line 568, in detect_language
return self.translator.detect(self.raw)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\site-packages\textblob\translate.py", line 72, in detect
response = self._request(url, host=host, type_=type_, data=data)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\site-packages\textblob\translate.py", line 92, in _request
resp = request.urlopen(req)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
response = meth(req, response)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
response = self.parent.error(
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
return self._call_chain(*args)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
result = func(*args)
File "C:\Users\*****\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
Process finished with exit code 1
I am still a little bit of a beginner in programming... Can I ask what I can do to solve this?
You can make it working by doing some changes in your translate.py file as mentioned below:
Original:
url = "http://translate.google.com/translate_a/t?client=webapp&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=0&kc=1"
Change above code to:
url = "http://translate.google.com/translate_a/t?client=te&format=html&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=0&kc=1"
For further details visit this link: "HTTPError: HTTP Error 404: Not Found" while using translation function in TextBlob
Since you only need to do language detection, I see that langdetect library is a good fit:
https://pypi.org/project/langdetect/
Here is a demo on how to use it:
>>> from langdetect import detect
>>> detect('هيا بنا نلعب')
'ar'
>>>
Problem
I'm using urllib.request.urlopen on the Wall Street Journal and it gives me a 404.
Details
Other sites work fine. Same error if I use https://. I did this example in REPL but the same error happens in my calls from my Django server:
>>> from urllib.request import urlopen
>>> urlopen('http://www.wsj.com')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
This is how it should work:
>>> urlopen('http://www.cbc.ca')
<http.client.HTTPResponse object at 0x10b0f8c88>
I'm not sure how to debug this. Anyone know what's going on, and how I can fix it?
first import Request like this:
from urllib.request import **Request**, urlopen
and then pass your url and header to Request like below:
url = 'https://www.wsj.com/'
response_obj = urlopen(Request(url, headers={'User-Agent': 'Mozilla/5.0'}))
print(response_obj)
I tested it now its working
Mod security blocks me from accessing a URL.
I have tried using other methods from this website, but none seem to work as I use an import called PyCurrency-Calculator
import PyCurrency_Converter
import urllib.request
from bs4 import BeautifulSoup
PyCurrency_Converter.convert(1, 'USD', 'A$')
The error is:
Traceback (most recent call last):
File "E:/Downloads/list of currencies.py", line 7, in <module>
PyCurrency_Converter.convert(1, 'USD', 'A$')
File "E:\Python34\lib\site-packages\PyCurrency_Converter\PyCurrency.py",
line 48, in convert
return PyCurrency.convert(amount, _from, _to)
File "E:\Python34\lib\site-packages\PyCurrency_Converter\PyCurrency.py",
line 31, in convert
response = urllib2.urlopen(url)
File "E:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "E:\Python34\lib\urllib\request.py", line 469, in open
response = meth(req, response)
File "E:\Python34\lib\urllib\request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "E:\Python34\lib\urllib\request.py", line 507, in error
return self._call_chain(*args)
File "E:\Python34\lib\urllib\request.py", line 441, in _call_chain
result = func(*args)
File "E:\Python34\lib\urllib\request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
PyCurrency_Converter makes a call google finance under the hood (code). This service is no longer available and so the library needs to be modified to use a different service.
What you can do is find an alternative web based api and call that (I have no recommendation). As you can see the code for PyCurrency_Converter is quite short, so it will be simple to take and adapt it to another currency conversion api or to use requests or urllib to just do the requests yourself.
The profanity checker seems to be working but does not return either (response: false) or response: true)
Here is a look at the code:
import urllib.request
def read_text():
quotes = open("/Users/sellis/Desktop/movie_quotes.rtf")
contents_of_file = quotes.read()
print(contents_of_file)
quotes.close()
check_profanity(contents_of_file)
def check_profanity(text_to_check):
connection = urllib.request.urlopen(" http://www.wdylike.com/profanity?q"+text_to_check)
output = connection.read()
print(output)
connection.close()
read_text()
When it is run it comes up as:
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\sl280\partightenfactor0
\f0\fs24 \cf2 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 -- Houston, we have a problem. (Apollo 13)\
\
-- Mama always said, life is like a box of chocolates. You never know what you are going to get. (Forrest Gump)\
\
-- You cant handle the truth. (A Few Good Men)\
\
-- I believe everything and I believe nothing. (A Shot in the Dark)\
}
Traceback (most recent call last):
File "/Users/sellis/Desktop/check_profanity.py", line 16, in <module>
read_text()
File "/Users/sellis/Desktop/check_profanity.py", line 8, in read_text
check_profanity(contents_of_file)
File "/Users/sellis/Desktop/check_profanity.py", line 11, in check_profanity
connection = urllib.request.urlopen(" http://www.wdylike.com/profanity?q"+text_to_check)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 532, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 505: HTTP Version Not Supported
After the code runs it is supposed to show a response: false or a response: true depending on if after reading the code the function discovers that there is a cuss word
Hello fellow Programmers,
today I wanted to get some JSON Data from this website using Python 3.3: http://ladv.de/api/-apikey-redacted-/ausDetail?id=884&wettbewerbe=true&all=true
The official API tells me that calling this URL returns some JSON Data. But if I use the following code to get it (which I found on stackoverflow, too), it throws an error:
import urllib.request
import json
request = 'http://ladv.de/api/mmetzger/ausDetail?id=884&wettbewerbe=true&all=true'
response = urllib.request.urlopen(request)
obj = json.load(response)
str_response = response.readall().decode('utf-8')
obj = json.loads(str_response)
print(obj)
prints out
Traceback (most recent call last):
File "D:/ladvclient/testscrape.py", line 5, in <module>
response = urllib.request.urlopen(request)
File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
return opener.open(url, data, timeout)
File "C:\Python33\lib\urllib\request.py", line 475, in open
response = meth(req, response)
File "C:\Python33\lib\urllib\request.py", line 587, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python33\lib\urllib\request.py", line 513, in error
return self._call_chain(*args)
File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
result = func(*args)
File "C:\Python33\lib\urllib\request.py", line 595, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
Where is the bug, and what is the correct code?
Thanks in advance,
forumfresser
The site you're trying to fetch is not available, as seen here:
http://ladv.de/api/-apikey-redacted-/ausDetail?id=884&wettbewerbe=true&all=true
You could also just read the error message by yourself:
urllib.error.HTTPError: HTTP Error 404: Not Found