Skip to content

Commit

Permalink
python web access
Browse files Browse the repository at this point in the history
  • Loading branch information
SyedaMairaSaad committed Dec 11, 2023
1 parent 196a333 commit d1c1bb6
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
51 changes: 51 additions & 0 deletions geoApi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import urllib.request, urllib.parse, urllib.error
import json
import ssl

api_key = False
# If you have a Google Places API key, enter it here
# api_key = 'AIzaSy___IDByT70'
# https://developers.google.com/maps/documentation/geocoding/intro

if api_key is False:
api_key = 42
serviceurl = 'http://py4e-data.dr-chuck.net/json?'
else :
serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

while True:
address = input('Enter location: ')
if len(address) < 1: break

parms = dict()
parms['address'] = address
if api_key is not False: parms['key'] = api_key
url = serviceurl + urllib.parse.urlencode(parms)

print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode()
print('Retrieved', len(data), 'characters')

try:
js = json.loads(data)
except:
js = None

if not js or 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
print(data)
continue

print(json.dumps(js, indent=4))

lat = js['results'][0]['geometry']['location']['lat']
lng = js['results'][0]['geometry']['location']['lng']
print('lat', lat, 'lng', lng)
place_id = js['results'][0]['place_id']
print(place_id)
27 changes: 27 additions & 0 deletions urljson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import urllib.request,urllib.parse,urllib.error
import json


import ssl
#ignore ssl certificate errrors
ctx=ssl.create_default_context()
ctx.check_hostname=ssl.CERT_NONE

#url=input('Enter- ')
url='https://py4e-data.dr-chuck.net/comments_1941001.json'
uh=urllib.request.urlopen(url)
data=uh.read().decode()
print('Retrieved',len(data),'characters')
try:
js=json.loads(data)
except:
js=None


comments=js['comments']
print('Count',len(comments))
count=0
for i in range(0,len(comments)):
count=count+comments[i]['count']
print('Sum',count)
27 changes: 27 additions & 0 deletions xmlParser3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
#py get-pip.py
#py -m pip help
#pip install beautifulsoup4cd
import urllib.request,urllib.parse,urllib.error
import xml.etree.ElementTree as ET

from bs4 import BeautifulSoup
import ssl
#ignore ssl certificate errrors
ctx=ssl.create_default_context()
ctx.check_hostname=ssl.CERT_NONE

url=input('Enter- ')
#url='http://py4e-data.dr-chuck.net/comments_42.html'
xml=urllib.request.urlopen(url,context=ctx).read()
xmlParse=ET.fromstring(xml)
dataTree=xmlParse.findall('comments/comment')
print('Retrieved',len(xml),'characters')
print('Count:',len(dataTree))

sum=0
for tree in dataTree:
c=tree.find('count').text
sum=sum+int(c)
print('Sum:',sum)

0 comments on commit d1c1bb6

Please sign in to comment.