Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions GeInfoFromAPIGoogle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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']
place = js['results'][0]['place_id']
print('lat', lat, 'lng', lng, 'id', place)
location = js['results'][0]['formatted_address']
print(location)
29 changes: 29 additions & 0 deletions GetInfoFromJSON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# import urllib library
from urllib.request import urlopen

# import json
import json
# store the URL in url as
# parameter for urlopen
url = "http://py4e-data.dr-chuck.net/comments_1327115.json"

# store the response of URL
response = urlopen(url)

# storing the JSON response
# from url in data
info = json.loads(response.read())

# print(json.dumps(info, indent=2))

#create variable to store the sum
num_list = list()

for item in info['comments']:
# print('Name', item['name'])
# print('count', item['count'])
#Store the value of content in num
num = float(item['count'])
num_list.append(num)

print(sum(num_list))
36 changes: 36 additions & 0 deletions RetrInfoFromSite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# To run this, download the BeautifulSoup zip file
# http://www.py4e.com/code3/bs4.zip
# and unzip it in the same directory as this file

from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl


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

url = 'http://py4e-data.dr-chuck.net/comments_1327112.html'
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")


#create variable to store the sum
num_list = list()

# Retrieve all of the anchor tags
tags = soup('span')
for tag in tags:
# Look at the parts of a tag
# print('TAG:', tag)
# print('URL:', tag.get('href', None))
# print('Contents:', tag.contents[0])
# print('Attrs:', tag.attrs)

#Store the value of content in num
num = float(tag.contents[0])
num_list.append(num)

print(sum(num_list))
14 changes: 14 additions & 0 deletions Socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
data = mysock.recv(512)
if len(data) < 1:
break
print(data.decode(),end='')

mysock.close()
File renamed without changes.