AttributeError: 'Response' object has no attribute 'json' - python

When I try to use the .json() method of a response object from the requests library, I get an error:
>>> import requests
>>> response = requests.get("http://example.com/myfile.json")
>>> response_json = response.json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Response' object has no attribute 'json'
Why am I getting this error and how can I fix it?

Your version of the requests library is too old. JSON support was added in version 0.12.1, released nearly 2 years ago.
The currently released version is 2.2.1.

Related

How to read Python json file?AttributeError: '_io.TextIOWrapper' object has no attribute 'load'

I am trying to load my json file. I am new to MacOS,but that should be the problem. My code
from terminal
import json
>>> with open('ticr_calculated_3_2020-05-27T11-01-30.json','r+', encoding='utf-8') as f:
data=json.load(f)
I got error
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'load'
The same error happens with json example from Python docs
json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.TextIOWrapper' object has no attribute 'dumps'
Why?
Have you written everything into one Python interpreter session? It seems to me that you defined the name json before.
Try opening a new Python interpreter or writing your script into a file.

Attribute Error : 'Response' object has no attribute 'css'

When I tried this I am getting an Attribute Error : 'Response' object has no attribute 'css'
I tried with this code :
response.css('h1.ctn-article-title::text').extract()
can anyone help please?
i'm trying to get text "Update Primary Care" from below code which is title :
Update Primary Care
CME
i'm placing my entire code :
response = requests.get(url, headers = headers)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'requests' is not defined
import requests
response = requests.get(url, headers = headers)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'url' is not defined
url = 'somethingurl'
response = requests.get(url, headers = headers)
response.css('h1.ctn-article-title::text').extract()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Response' object has no attribute 'css'
response.css('h1').extract()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Response' object has no attribute 'css'
response.css('h1.ctn-article-title::text').extract()
As Tarun pointed out in the comments: You are mixing scrapy and requests code.
If you want to create a scrapy response from requests response you can try:
from scrapy.http import TextResponse
import requests
url = 'http://stackoverflow.com'
resp = requests.get(url)
resp = TextResponse(body=resp.content, url=url)
resp.xpath('//div')
# works!
See the docs for requests.Response and scrapy.http.TextResponse objects.
In this case the line where your error occurs expects a CSSResponse object not a normal response. Try to create a CSSResponse instead of the normal Response to resolve the error.
You can get it here
More specifically use an HtmlResponse because your response would be some HTML and not plain text. HtmlResponse is a subclass of CSSResponse so it inherits the missing method.
add this line in your code and it will work fine
remove any imports for requests from any other package.
from scrapy.http import Request

AttributeError: 'module' object has no attribute 'TextCalendar'

When I copy the below code in Ideone then it runs fine but it is not running in the text editor and showing the error mentioned in the subject of this post:
import calendar
c = calendar.TextCalendar(calendar.SUNDAY)
c.prmonth(2007, 7)
The complete error is:
Traceback (most recent call last):
File "calendar.py", line 1, in <module>
import calendar
File "/home/shl/Desktop/calendar.py", line 2, in <module>
c = calendar.TextCalendar(calendar.SUNDAY)
AttributeError: 'module' object has no attribute 'TextCalendar'
change the program name from calendar.py to something like calendar_code.py
This wold work properly.
Importing from builtin library when module with same name exists

Weather API - 'NoneType' object is not subscriptable

heres my problem I'm trying to write a script to get the weather for my location here is the code
import requests
url = ("http://api.worldweatheronline.com/free/v1/weather.ashx?key=hfvb4qmehh8g9p8krcbmj8ew&q=48.85,2.35&fx=no&format=json")
r = requests.get(url)
forecast = r.json
print (forecast)["data"]["current_condition"]["temp_F"]
heres the error
<bound method Response.json of <Response [200]>>
Traceback (most recent call last):
File "C:\Users\Grant\Desktop\weather.py", line 6, in <module>
print (forecast) ["data"]["current_condition"]["temp_F"]
TypeError: 'NoneType' object is not subscriptable
any help would be appreciated
print (forecast) ["data"]["current_condition"]["temp_F"]
indexes the result of the print() call. That's probably not what you want.
Try
print(forecast["data"]["current_condition"]["temp_F"])
instead.

python-spidermonkey object has no attribute 'eval_script' error

Using python-spidermonkey in the following way (following the guide) gives me the error AttributeError: 'spidermonkey.Context' object has not attribute 'eval_script'.
>>> import spidermonkey
>>> rt = spidermonkey.Runtime()
>>> cx = rt.new_context()
>>> cx.eval_script("1 + 2") + 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'spidermonkey.Context' object has no attribute 'eval_script'
I experienced the same problem and I have also found this bug report:
https://launchpad.net/~pi-rho/+archive/security/+build/3866138
Installing this package on Ubuntu (12.10) worked for me:
https://launchpad.net/~pi-rho/+archive/security/+build/3866138

Categories

Resources