import requests
import json
s = requests.Session()
s.params["key"] = "MY_KEY"
s.params["cx"] = "MY_CX"
s.params["num"] = 1
result = s.get('https://www.googleapis.com/customsearch/v1', params={"q": "Search query"})
Everything is working fine and I do get a result back but it's JSON. What I want is one thing. Since my result only gives 1 search result, I want the link of that search result. From I've understood that link value is under the keys "item" and then "link". I've tried lots of things but I keep getting either one of these errors below.
TypeError: 'Response' object is not subscriptable
NameError: name 'json' is not defined
AttributeError: 'Response' object has no attribute 'read'
TypeError: the JSON object must be str, not 'Response'
What am i doing wrong and what's the solution?
Related
Do you know repl.it?
I am coding python on this site.
And my goal is creating Web Scraper.
I think this code is clean.
But I'm getting an error:
AttributeError: 'function' object has no attribute 'text'
My code:
import requests
indeed_result = requests.get
("https://kr.indeed.com/jobs?q=python&l=%EC%9D%B8%EC%B2%9C")
print(indeed_result.text)
Surely, I have requests package installed.
Please give me some advice
You just need to remove the back to new line after get like this:
import requests
indeed_result = requests.get("https://kr.indeed.com/jobs?q=python&l=%EC%9D%B8%EC%B2%9C")
print(indeed_result.text)
if you want to continue typping in the next line just add a backslash \ as follows:
indeed_result = requests.get\
("https://kr.indeed.com/jobs?q=python&l=%EC%9D%B8%EC%B2%9C")
Removing back to new line after get
try this
import requests
res = requests.get("https://kr.indeed.com/jobs?q=python&l=%EC%9D%B8%EC%B2%9C")
print(res.text)
# result if success 200
I have used this function before and it worked perfectly. However, I may have accidentally changed something and it now returns the error:
TypeError: 'module' object is not callable
For the line:
bom = botometer.botometer(wait_on_ratelimit=True,rapidapi_key=rapidapi_key,**twitter_app_auth)
The full code I use is:
def bot_detector(account,lang='universal'):
consumer_secret = deleter(open('consumer_sxcrxt.txt','r').rxad(),'\n')
consumer_key = deleter(open('api.txt','r').read(),'\n')
twitter_app_auth = {'consumer_key': consumer_key,'consumer_secret': consumer_secret}
rapidapi_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
bom = botometer.botometer(wait_on_ratelimit=True,rapidapi_key=rapidapi_key,**twitter_app_auth)
result = bom.check_account(account)
score = result['cap'][lang]
return score
print(bot_detector(1.25948029617448E+018))
Notes:
'deleter' is just a way for me to remove the line separator on the files containing my api keys.
I just checked and my twitter api keys are working.
If I put botometer.Botometer as it says on the documentation, I get the error:
AttributeError: module 'botometer' has no attribute 'Botometer'
(I think they made a typo on the documentation.)
Documentation:
https://libraries.io/pypi/botometer
I named my file 'botometer.py' and thus python was looking in the wrong place.
When writing the following code, the console shows an error:
AttributeError: 'NoneType' object has no attribute 'group'
The code:
req = re.search("([0-9]+)([A-Za-z]+)", data).group(0)
But, when debugging I see that there is a group, and the code continues to run instead of collapsing. For example when data is "30DIR /Users/user1/Documents/", the console outpost an error while debugging shows there is a match: "30DIR".
req = re.search("([0-9]+)([A-Za-z]+)", data)
if req:
req = req.group(0)
I totally new in Python and stuck with this error:
I am trying to encode an image and then convert it to json to upload into a no-sql db. But when I am trying to convert it to json I am getting this error:
"AttributeError: 'bytes' object has no attribute 'dict'"
Below is my python code:
import base64
import json
def jsonDefault(object):
return object.__dict__
with open("img123.png", "rb") as imageFile:
str = base64.b64encode(imageFile.read())
print(str)
json_str = {'file_name':'img123','img_str':str}
pytojson = json.dumps(json_str, default=jsonDefault)
print(pytojson)
That happens because you are trying to access a property that a bytes object doesn't have (__dict__). I understand that you need to return a format that can be serialized to JSON.
This works for me, but I don't know if it's the decoding you desire:
def jsonDefault(o):
return o.decode('utf-8')
See TypeError: b'1' is not JSON serializable as well.
I use python and this is my code
myUser = 'username'
myServer = 'http://api.url.net', "{\"orga\":\"monorga\",\"coupon\":\"moncoupon\"}"
myPass = 'pass'
authString = base64.encodestring('%s:%s' % (myUser, myPass))
headers = {'Authorization':"Basic %s" % authString}
req = urllib2.Request(myServer, None, headers)
openedUrl = urllib2.urlopen(req)
url = url.strip()
AttributeError: 'tuple' object has no attribute 'strip'
Please help me
When you write url = 'https://api.url.net', "{\"orga\":\"monorga\",\"coupon\":\"moncoupon\"}", basically you're just creating a tuple of two elements ('https://api.url.net' and "{\"orga\":\"monorga\",\"coupon\":\"moncoupon\"}"), and assigning a reference to this tuple to the url variable. This is due to the fact that, in python, there is not always the need to write the parenthesis of a tuple: a = 1, 2 for example.
Thus, url is now a tuple. Also, a tuple does not have a strip method, so you can't call url.strip.
To call strip on url, you must first convert it to a string.
I found this question whilst encountering this error message.
In my case, the problem was that I had an errant trailing comma on a line declaring a string, like so:
x = "my string",
x is therefore, I think quite strangely, a tuple of length one. First and only element is "my string".
This strikes me as an easy and confusing error to make, so I thought I'd put this answer here.