Tried to upvote a reddit submission but received a 404 HTTP response - python

I'm new to coding and I suck. I hope to find help here.
I was playing around with the praw module for reddit and couldn't get the simple function upvote() to work.
Here's my code:
import praw
r = praw.Reddit(client_id='**************',
client_secret='**************',
user_agent='**************',
username='**************',
password='***********')
r.submission('https://www.reddit.com/r/aww/comments/9znyf2/today_is_my_18th_birthday_everyone/').upvote()
And here's the full error:
Traceback (most recent call last):
File "C:\Users\*****\Desktop\*****\*****\*****\*****\*****\A.py", line 11, in <module>
r.submission('https://www.reddit.com/r/aww/comments/9znyf2/today_is_my_18th_birthday_everyone/').upvote()
File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\praw\models\reddit\mixins\votable.py", line 80, in upvote
self._vote(direction=1)
File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\praw\models\reddit\mixins\votable.py", line 10, in _vote
'id': self.fullname})
File "C:\Users*****\AppData\Local\Programs\Python\Python37\lib\site-packages\praw\reddit.py", line 465, in post
params=params)
File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\praw\reddit.py", line 506, in request
params=params)
File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\prawcore\sessions.py", line 185, in request
params=params, url=url)
File "C:\Users\*****\AppData\Local\Programs\Python\Python37\lib\site-packages\prawcore\sessions.py", line 130, in _request_with_retries
raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.NotFound: received 404 HTTP response

PRAW is trying to use that URL as a post ID, not as a URL. url is the second argument to reddit.submission(), so you'll need to use it as a named argument (r.submission(url='https://www.reddit.com/...')) to get the submission. After you do that everything should work as expected.

Related

Why am I getting an error when using the requests module to return the server code?

(I used a translator when writing this article. Please understand that some words may be incorrect.)
I tested it using the requests module. If the site cannot be found, a 404 code should be returned, but with an error. I don't know what the reason is. Any help would be appreciated. How to properly return a 404 code?
---Below is the code.
import requests as re
a = re.get(input())
print(a.status_code)
error :
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\82104_dvfqr9f\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\82104_dvfqr9f\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\82104_dvfqr9f\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 515, in request
prep = self.prepare_request(req)
File "C:\Users\82104_dvfqr9f\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 443, in prepare_request
p.prepare(
File "C:\Users\82104_dvfqr9f\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 318, in prepare
self.prepare_url(url, params)
File "C:\Users\82104_dvfqr9f\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 392, in prepare_url
raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL 'eeee.com': No scheme supplied. Perhaps you meant http://eeee.com?
You can use this link for how to work with requests module.
import requests
try:
r = requests.get('https://www.google.com/search?q=ggg')
print(r.status_code)
if r.status_code==404:
print("this url dosn't exist")
except Exception as error:
print(error)

Requests. __init__() got an unexpected keyword argument 'strict'

I need your help.
I get this error using requests.get():
__init__() got an unexpected keyword argument 'strict'
I found only one piece of advice online which is to update the requests library, but it did not change anything.
Python 3.9, Requests 2.27.1.
print(requests.__version__)
2.27.1
Here is a code:
import requests
url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=###########'
r = requests.get(url)
Here is a Traceback
r = requests.get(url)
Traceback (most recent call last):
File "C:\Users\OLEG~1.KAZ\AppData\Local\Temp/ipykernel_13156/3951091591.py", line 1, in <module>
r = requests.get(url)
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 335, in request
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 438, in send
auth = request.auth
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 282, in send
response.url = req.url
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 423, in urlopen
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 238, in _get_conn
File "C:\Users\oleg.kazanskyi\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 560, in _new_conn
TypeError: __init__() got an unexpected keyword argument 'strict'
Does anyone have a fix?
Thank you, #deceze, for the piece of advice.
I removed the XMLA and Requests libraries and reinstalled both as it's described here:
github.com/psf/requests/issues/5959
Now it works.
For whatever reason, I did it twice to make it work.

Unable to read URL pandas_datareader

import pandas as pd
import pandas_datareader as web
spy_etf = web.DataReader('SPY','google')
spy_etf.info()
#ERROR
/Users/mac/anaconda3/envs/pyfinance/bin/python /Users/mac/Desktop/M_BOT_AND_TOOLS/Anaconda/PYTHON/_root_exercises/udemy_class_python_for_finance/jobs/Test/test_1.py
Traceback (most recent call last):
File "/Users/mac/Desktop/M_BOT_AND_TOOLS/Anaconda/PYTHON/_root_exercises/udemy_class_python_for_finance/jobs/Test/test_1.py", line 4, in <module>
spy_etf = web.DataReader('SPY','google')
File "/Users/mac/anaconda3/envs/pyfinance/lib/python3.6/site-packages/pandas_datareader/data.py", line 137, in DataReader
session=session).read()
File "/Users/mac/anaconda3/envs/pyfinance/lib/python3.6/site-packages/pandas_datareader/base.py", line 181, in read
params=self._get_params(self.symbols))
File "/Users/mac/anaconda3/envs/pyfinance/lib/python3.6/site-packages/pandas_datareader/base.py", line 79, in _read_one_data
out = self._read_url_as_StringIO(url, params=params)
File "/Users/mac/anaconda3/envs/pyfinance/lib/python3.6/site-packages/pandas_datareader/base.py", line 90, in _read_url_as_StringIO
response = self._get_response(url, params=params)
File "/Users/mac/anaconda3/envs/pyfinance/lib/python3.6/site-packages/pandas_datareader/base.py", line 139, in _get_response
raise RemoteDataError('Unable to read URL: {0}'.format(url))
pandas_datareader._utils.RemoteDataError: Unable to read URL: http://www.google.com/finance/historical?q=SPY&startdate=Jan+01%2C+2010&enddate=Aug+17%2C+2018&output=csv
Process finished with exit code 1
Not being able to acess google financial historial data
The problem is not with your code, but google's support of your query.
If you were to manually GET the generated url:
import requests
requests.get('http://www.google.com/finance/historical?q=SPY&startdate=Jan+01%2C+2010&enddate=Aug+17%2C+2018&output=csv')
# results in <Response [403]>
You will notice the url results in a 403 response, meaning access to the url is forbidden. As a result you get the pandas_datareader._utils.RemoteDataError: Unable to read URL

Getting error when using praw to login to reddit

I've seen a couple of questions that already asked this but there were no responses, so I'll give it a try. When I use the following code:
import praw, time
r = praw.Reddit(user_agent="Bot experiment by redacted")
r.login('redacted', 'redacted')
I get a connection error that has the following traceback:
Traceback (most recent call last):
File "redacted", line 5, in <module>
r.login('redacted', 'redacted')
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/praw/__init__.py", line 1263, in login
self.request_json(self.config['login'], data=data)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/praw/decorators.py", line 161, in wrapped
return_value = function(reddit_session, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/praw/__init__.py", line 519, in request_json
response = self._request(url, params, data)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/praw/__init__.py", line 383, in _request
_raise_response_exceptions(response)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/praw/internal.py", line 172, in _raise_response_exceptions
response.raise_for_status()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py", line 831, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden
I have tried this from python 3.4 and 2.7, I've tried running from IDLE and from the terminal. I've tried leaving my username and password out and logging in when prompted. I've tried from my Mac in my hotel room and a Windows machine from work and I get the same error everytime. I've tried from my bot account that I just made and my normal account. Does anyone have any ideas?
The issue was that I had the word 'bot' in my user_agent string. After it was removed, there were no problems.

prestapyt error on edit

I'm using the prestashop API prestapyt
I add a category by hand in my site, but when I'm trying to edit via the API, it fails.
Even the most simple thing, does not work:
ps = PrestaShopWebServiceDict(config.DOMAIN, config.KEY)
xml = ps.get('categories', 35)
ps.edit('categories',35,xml)
The xml (type DICT) loads ok, but the .edit() fails.
Here is the response
Traceback (most recent call last):
File "C:\Users\noukeys\Documents\psapi\main.py", line 31, in <module>
ps.edit('categories',35,xml)
File "C:\Python27\lib\site-packages\prestapyt\prestapyt.py", line 333, in edit
return self.edit_with_url(full_url, content)
File "C:\Python27\lib\site-packages\prestapyt\prestapyt.py", line 452, in edit_with_url
return super(PrestaShopWebServiceDict, self).edit_with_url(url, xml_content)
File "C:\Python27\lib\site-packages\prestapyt\prestapyt.py", line 344, in edit_with_url
return self._parse(self._execute(url, 'PUT', body=unicode_encode.encode(content), add_headers=headers)[2])
File "C:\Python27\lib\site-packages\prestapyt\prestapyt.py", line 171, in _execute
self._check_status_code(status_code)
File "C:\Python27\lib\site-packages\prestapyt\prestapyt.py", line 125, in _check_status_code
% (status_code, message_by_code[status_code]), status_code)
prestapyt.prestapyt.PrestaShopWebServiceError: 'This call to PrestaShop Web Services failed and returned an HTTP status of 400. That means: Bad Request.'
I'm having issues with edits also, but I am getting a 501 error. Turns out my host does not allow PUT requests.
You can turn on debugging in the api.
prestashop.debug = True
And then turn on display_errors in config.inc.php for your shop. This helped me debug why the request was bad. The error messages are pretty clear.

Categories