-
-
Save P1xt/c0c98b12e1ba22716f60b978ac486f55 to your computer and use it in GitHub Desktop.
The python program only publishes the first item in the query. I'd like to iterate through all items and publish them to HTML but can't figure out how.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Practicing call to DPLA and getting data from it, then displaying results via html | |
import webbrowser | |
from operator import itemgetter | |
import requests | |
f=open('result.html', 'w') | |
# Make an api call and store the response. | |
url = "https://api.dp.la/v2/items?q=penguins&api_key=aefc7b2874411888e4e06b515935c19c" | |
r = requests.get(url) | |
print('Status code: ', r.status_code) | |
# Process information from each item | |
data = r.json() | |
data_list = [] | |
# Everything before the list goes in header | |
header = """<html> | |
<head> | |
<title> Practice </title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"></link> | |
<style> | |
body { margin-top: 20px; margin-left: 20px;} | |
h1 { } | |
</style> | |
</head> | |
<body> | |
<h1> Practicing Python Queries to HTML Data </h1>""" | |
# put the list in the docs variable | |
docs = "" | |
for d in data['docs']: | |
# print('Title:', d['sourceResource']['title']) | |
# print('Source:', d['dataProvider']) | |
# print('URL:', d['isShownAt']) | |
title = d['sourceResource']['title'] | |
source =d['dataProvider'] | |
link = d['isShownAt'] | |
message = """<p><strong>Title: </strong><a href="%s"> %s</a><br><strong>Location: </strong>%s</p>""" | |
# pass results from python query to html display | |
result = message % (link, title, source) | |
docs += result | |
# put everyting after the list in footer | |
footer = """</body> | |
</html>""" | |
# write all data to html file | |
f.write(header) | |
f.write(docs) | |
f.write(footer) | |
f.close() | |
webbrowser.open_new_tab('result.html') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- This is the html file created from the above python file --> | |
<html> | |
<head> | |
<title> Practice </title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"></link> | |
<style> | |
body { margin-top: 20px; margin-left: 20px;} | |
h1 { } | |
</style> | |
</head> | |
<body> | |
<h1> Practicing Python Queries to HTML Data </h1> | |
<p><strong>Title: </strong><a href="http://digitalcollections.nypl.org/items/510d47db-dcac-a3d9-e040-e00a18064a99"> Poems for penguins</a><br><strong>Location: </strong>General Research Division. The New York Public Library</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Refactored to not rewrite the entire html for each item in the docs list retrieved.