Skip to content

Instantly share code, notes, and snippets.

@syslo
Last active July 20, 2016 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syslo/75f430c310c101769d3705855654e97c to your computer and use it in GitHub Desktop.
Save syslo/75f430c310c101769d3705855654e97c to your computer and use it in GitHub Desktop.
Materiál k prednáške na Letnej Škole KSP 20.7.2016

Dáta na internete

Prednáška bude v jazyku Python, hrať sa môžete tu: https://repl.it/languages/python3

Dátové typy

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)

Sťahovanie

Chceme v programe stiahnuť http://sme.sk/robots.txt. Ako na to?

Toto nefunguje online! Treba robiť na počítači. A stiahnuť si: http://docs.python-requests.org

import requests  # musia byť nainštalované!

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

Fajn zdroje dát:

Google Geolocation

Kalendáre

Facebook Graph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment