Advertisement
Guest User

Untitled

a guest
May 11th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import urllib.request, json
  2. import pymysql.cursors
  3.  
  4. cnx = pymysql.connect(user="bookzi_dk",
  5. password="thispasswordisbad",
  6. host='mysql73.unoeuro.com',
  7. database='bookzi_dk_db')
  8. cursor = cnx.cursor()
  9.  
  10. for i in range(14059, 99998):
  11. if i < 10000:
  12. print(i)
  13. try:
  14. with urllib.request.urlopen("http://api.zippopotam.us/DE/0" + str(i)) as url:
  15. data = json.loads(url.read().decode())
  16. query = ('INSERT INTO zipcode (zipcode, City) VALUES (%s, %s);')
  17. zipcode = data['post code']
  18. city = data['places'][0]['place name']
  19. cursor.execute(query, (zipcode, city))
  20. cnx.commit()
  21. except:
  22. print("no postnumber found!")
  23.  
  24. elif i <= 99998:
  25. print(i)
  26. try:
  27. with urllib.request.urlopen("http://api.zippopotam.us/DE/" + str(i)) as url:
  28. data = json.loads(url.read().decode())
  29. query = ('INSERT INTO zipcode (zipcode, City) VALUES (%s, %s);')
  30. zipcode = data['post code']
  31. city = data['places'][0]['place name']
  32. cursor.execute(query, (zipcode, city))
  33. cnx.commit()
  34. except:
  35. print("no postnumber found!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement