Skip to content

Instantly share code, notes, and snippets.

@syslo
Created March 31, 2017 13:35
Show Gist options
  • Save syslo/4cae03b786661e2267bc565aa5a72aea to your computer and use it in GitHub Desktop.
Save syslo/4cae03b786661e2267bc565aa5a72aea to your computer and use it in GitHub Desktop.
Používateľ Internetu ++

Internetový používateľ ++

Python

Na skúšanie príkazov prednášky odporúčam prázdny python sandbox:

python3 -m venv venv
source venv/bin/activate
pip install requests
python

Dátové formáty

JSON

http://www.w3schools.com/json/

{
  "students":[
    {
      "firstName": "John",
      "lastName": "Doe",
      "age": 15,
      "grades": {
        "math": [1, 1, 3, 2],
        "history": [3, 3, 4, 2]
      }
    },
    {
      "firstName": "Anna",
      "lastName": "Smith",
      "grades": {
        "math": [],
        "physics": [1, 1]
      },
      "age": 14
    },
    {
      "firstName": "Peter",
      "lastName": "Jones",
      "age": 14,
      "grades": {
        "history": [1, 2, 1]
      }
    }
  ]
}
import json

str_data = '{"students": [{"grades": {"history": [3, 3, 4, 2], "math": [1, 1, 3, 2]}, "lastName": "Doe", "firstName": "John", "age": 15}, {"grades": {"physics": [1, 1], "math": []}, "lastName": "Smith", "firstName": "Anna", "age": 14}, {"grades": {"history": [1, 2, 1]}, "lastName": "Jones", "firstName": "Peter", "age": 14}]}'

data = json.loads(str_data)

print(data["students"][2]["firstName"] + " " + data["students"][2]["lastName"])
peters_grades = data["students"][2]["grades"]["history"]
print(sum(peters_grades)/len(peters_grades))

data["students"][1]["grades"]["math"].append(1)
print(data["students"][1]["grades"])

new_str_data = json.dumps(data)
print(len(new_str_data))

XML

http://www.w3schools.com/xml/

<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>Thick slices made from our homemade sourdough bread</description>
    <calories>600</calories>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    <calories>950</calories>
  </food>
</breakfast_menu>
from xml.etree.ElementTree import fromstring, tostring

str_data = '<breakfast_menu><food><name>Belgian Waffles</name><price>$5.95</price><description>Our famous Belgian Waffles with plenty of real maple syrup</description><calories>650</calories></food><food><name>French Toast</name><price>$4.50</price><description>Thick slices made from our homemade sourdough bread</description><calories>600</calories></food><food><name>Homestyle Breakfast</name><price>$6.95</price><description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description><calories>950</calories></food></breakfast_menu>'

data = fromstring(str_data)

for food in data:
	print(food[0].text)
	print(food.find('price').text)
	print()

data[1].find("name").text = "Slovak Toast"

new_str_data = tostring(data)
print(new_str_data)

Zdroje dát

https://github.com/toddmotto/public-apis

HTTP Protokol

import requests

response = requests.get('http://sme.sk/robots.txt')
print(response.text)

response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=Lucky+1392+Mocenok')
data = response.json()
first_result = data['results'][0]
print(first_result['geometry']['location'])

response = requests.post('https://hooks.slack.com/services/...', json={"text": "Ahoj"})
response.text

Osnova k druhej časti

  • HTTP:
    • Request
    • Response
    • URL
    • Query parameters
    • Verb
    • Body
    • Status Code
    • Headers
    • Cookies
  • Prehliadač
    • HTML
    • CSS + resources
    • JavaScript (Princíp, Izolácia, Budúcnosť)
    • Pôvodné riešenia (HTML Form, Flash, Java Applet)
    • HTTPS
    • Autorizácia (Basic Auth, Cookies, OAuth protokol, Certifikáty)
    • JavaScript, Cookies a súkromie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment