AttributeError: 'module' object has no attribute 'HTTPError' - python

I made a Worksheet class using the spread python library. here is my code:
from oauth2client.service_account import ServiceAccountCredentials
import time
import gspread
import requests
import os
DIRNAME = os.path.dirname(__file__)
class Wksh:
credentials_google_sheet = ServiceAccountCredentials.from_json_keyfile_name(os.path.join(DIRNAME, 'credentials/gs_credentials.json'),['https://spreadsheets.google.com/feeds'])
gc = gspread.authorize(credentials_google_sheet)
worksheet_id = None
sheet_name = None
sheet = None
def __init__(self,wksh_id,sh_name):
while True:
try:
self.worksheet_id = wksh_id
self.sheet_name = sh_name
self.sheet = self.gc.open_by_key(wksh_id).worksheet(sh_name)
break
except (gspread.exceptions.HTTPError, gspread.exceptions.requests, gspread.exceptions.RequestError) as e:
if e[0] == 401:
self.gc.login()
elif e[0] == 500:
time.sleep(10)
else:
print 'init'
print e
break
except requests.exceptions.ConnectionError as e:
time.sleep(10)
except requests.exceptions.ChunkedEncodingError as e:
time.sleep(20)
def write(self,cell,value):
while True:
try:
self.sheet.update_acell(cell,value)
break
except (gspread.exceptions.HTTPError, gspread.exceptions.requests, gspread.exceptions.RequestError) as e:
if e[0] == 401:
self.gc.login()
elif e[0] == 500:
time.sleep(10)
else:
print 'write'
print e
break
except requests.exceptions.ConnectionError as e:
time.sleep(10)
except requests.exceptions.ChunkedEncodingError as e:
time.sleep(20)
This works fine but whenever there is a request error, even though I try to catch the error I get:
AttributeError: 'module' object has no attribute 'RequestError' or
AttributeError: 'module' object has no attribute 'HTTPError'
Why is this happening? Do I need to use self. before the exceptions?
Thanks

HTTPError was removed check gspread changes https://github.com/burnash/gspread/blob/87583c562dd3951122411f7029963e53ef16a610/HISTORY.rst#062-2016-12-20
Here you can see all available exceptions
https://github.com/burnash/gspread/blob/master/gspread/exceptions.py

Related

problem with xpath i'm getting invalid url trying to scrap

I have the following problem with my code. I'm trying to scrap information of the next link
sepomex
I'm trying to get the Estado, Municipio and a list of cp (código postal btw) but I'm getting this:
enter image description here
and this is my code:
import os
import datetime
import requests
import lxml.html as html
HOME_URL = 'https://www.correosdemexico.gob.mx/SSLServicios/ConsultaCP/Descarga.aspx'
XPATH_ESTADOS = '//select[#id="DdlEstado"]/option[#value > 0]/text()'
XPATH_MUNICIPIOS = '//select[#id="DdlMunicipio"]/option[#value > 0]/text()'
XPATH_LOCALIDAD = '//table[#class="small"]/tbody/tr[#class="dgNormal"]/td/text()'
def parse_edos(estado, today):
try:
response = requests.get(estado)
if response.status_code == 200:
root = html.fromstring(response.content)
try:
municipios = root.xpath(XPATH_MUNICIPIOS)
for municipio in municipios:
localidad = root.xpath(XPATH_LOCALIDAD)
except IndexError:
return
with open(f'{today}/{estado}.txt', 'w', encoding='utf-8') as f:
for i in localidad:
f.write(i + '\n')
else:
raise ValueError(f'Error: {response.status_code}')
except ValueError as err:
print(err)
def main():
try:
response = requests.get(HOME_URL)
if response.status_code == 200:
root = html.fromstring(response.content)
estados = root.xpath(XPATH_ESTADOS)
today = datetime.datetime.now().strftime('%Y-%m-%d')
if not os.path.isdir(today):
os.mkdir(today)
for estado in estados:
parse_edos(estado, today)
else:
raise ValueError(f'Error: {response.status_code}')
except ValueError as err:
print(err)
def run():
main()
if __name__ == '__main__':
run()
srry about my kewl english :P

Syntax error in module 'lambda_function': unindent does not match any outer indentation level (lambda_function.py, line 26)

Syntax error in module 'lambda_function': unindent does not match any outer indentation level (lambda_function.py, line 26)
from _future_ import print_function
import json
import boto3
import time
import urllib
print('loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'])
try:
print("Using waiter to waiting for object to persist thru s3 service")
waiter = s3.get_waiter('object_exists')
waiter.wait(Bucket+bucket, Key=key)
response = s3.head_object(Bucket=bucket, Key=key)
print("CONTENT TYPE: " +response['ContentType'])
print("Etag: " + response['ETag'])
print("Content-Length: ", response['ContentLength'])
print("Keyname: "+key)
print("Deleting object" + key)
s3.delete_object(Bucket=bucket, Key=key)
return response['ContentType']
except Exception as e
print(e)
print('Error getting object{} from bucket {} . Make sure they exist'
'bucket is in same region as this is'
'function.' ,format(key, bucket))
raise e
Your indentation is incorrect, just like the exception says.
def lambda_handler(event, context):
# indented by 3 spaces
try:
...
# indented by 4 spaces ...
except Exception as e # NOTE: Missing `:`
...
Your code example is also missing a : at the end of the exception handler
Right here:
print("Content-Length: ", response['ContentLength'])
print("Keyname: "+key)
print("Deleting object" + key)
s3.delete_object(Bucket=bucket, Key=key)
return response['ContentType']
except Exception as e: #<---also add me, the colon :)
print(e)
print('Error getting object{} from bucket {} . Make sure they exist'
'bucket is in same region as this is'
'function.' ,format(key, bucket))
raise e
The indent of the stuff after the except needs to be moved back one space.

error: 'str' object has no attribute 'read'

Hey there!
# import the module
from __future__ import print_function
import aerospike
import urllib2
import json
config = {
'hosts': [ ('127.0.0.1', 3000) ]
}
try:
client = aerospike.client(config).connect()
except:
import sys
print("failed to connect to the cluster with", config['hosts'])
sys.exit(1)
key = ('ip', 'hit', 'trial')
try:
for i in range(0,255):
for j in range(0,255):
for k in range(0,255):
for l in range(0,255):
if not((i == 198 and j == 168) or (i == 172 and j > 15 and j < 32) or (i == 10)):
response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l)).read()
html = response.read()
client.put(key, json.load(response))
except Exception as e:
import sys
print("error: {0}".format(e), file=sys.stderr)
client.close()
Error
error: 'str' object has no attribute 'read'
Can anyone please help in resolving this error? Any help would be highly appreciated.
You've already called read() here:
response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l)).read()
And then you are trying to call read() again:
html = response.read()
Also, you are using json.load() afterwards which accepts a file-object, not a string. Either do:
response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l)).read()
client.put(key, json.loads(response))
Or:
response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l))
client.put(key, json.load(response))

String indices must be integers in one place of code in python

import pymongo
import sys
client=pymongo.MongoClient('localhost',27017)
db=client.rop
try:
cntcur=db.albums.aggregate([{"$unwind":"$images"},"$group":"_id":"null","count":{'$sum':1}}}])
cursor1=db.images.find()
for im in cursor1:
id1=int(im['_id'])
cnt=0
cursor= db.albums.aggregate([{"$unwind":"$images"}])
print id1
for image in cursor:
print "moving to images collection"
ig=image['images']
if (id1 == ig):
break;
else:
cnt=cnt+1
if (cnt == cntr):
print "removing"
db.images.remove({'_id':id1})
except Exception as e:
print "unexpected error", type(e),e
I get error in the line ig=image['images']. I tried changing it to ig=int(image['images']), but I get same error.
import pymongo
import sys
client = pymongo.MongoClient('localhost',27017)
db=client.rop
try:
cursor1=db.images.find()
for im in cursor:
id1=int(im['_id'])
print id1
cnt=0
cursor=db.albums.find()
for image in cursor:
ig=image['images']
for data in ig:
if (id1 == data):
cnt=1
break;
if cnt==0:
print "removing"
db.images.remove({'_id':id1})
except Exception as e:
print "unexpected error",type(e) , e

Python: Handling requests exceptions the right way

I recently switched from urlib2 to requests and I'm not sure how to deal with exceptions. What is best practice? My current code looks like this, but is not doing any good:
try:
response = requests.get(url)
except requests.ConnectionError , e:
logging.error('ConnectionError = ' + str(e.code))
return False
except requests.HTTPError , e:
logging.error('HTTPError = ' + str(e.reason))
return False
except requests.Timeout, e:
logging.error('Timeout')
return False
except requests.TooManyRedirects:
logging.error('TooManyRedirects')
return False
except Exception:
import traceback
logging.error('generic exception: ' + traceback.format_exc())
return False
Since it looks bad as a comment, have you tried:
try:
# some code
except Exception as e:
print e

Categories