0

I'm trying to aggregate pieces from different websites and consolidate them into a single site.

Since the information will change daily, I was trying to output the HTML code dynamically with %s formatting. However, this seems ripe for disaster somewhere along the way as the amount of information grows since I don't know of any way I can label the %s to at least keep track of what they're referring to (This is only a fraction of what I would like to ultimately have).

I tried looking at Bootstrap, but it was a little beyond me honestly as all I'm looking to do is have a simple HTML output. Is that the best solution at this point?

url = 'http://rss.nytimes.com/services/xml/rss/nyt/Business.xml' resp = requests.get(url) soup = BeautifulSoup(resp.content, features = 'xml') items = soup.findAll('item') print(items) all_items = '' wrapper = """ <html> <header> Daily Monitor </header> <body> <p>URL: <a href=\"%s\">%s</a></p> <p> %s </p> </body> </html>""" x = wrapper % ('What', 'to', 'do') #This is the data I would like to eventually incorporate into the HTML - not used here in the example for item in items[:10]: all_items += item.title.text print(item.description.text) print(item.link) #this is how I'm writing out the HTML filename = 'C:\\test' + '.html' f = open(filename,'w') f.write(x) f.close() 

1 Answer 1

4

You should use the new style of string substitution, with .format(). It will allow you to have a much more readable template string.

Consider the following:

all_items = { 'title': 'Google', 'link': 'https://google.com', 'description': 'Hey, have you heard about this new search engine?' } wrapper = """ <html> <header> Daily Monitor </header> <body> <p>URL: <a href=\"{link}\">{title}</a></p> <p> {description} </p> </body> </html>""" x = wrapper.format(**all_items) 

And here it is in action.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice one - Thanks. Going to mess around with this - I have a lot of sites so thinking how I can cleverly build the dictionary out as a loop - thanks!
No problem. I use .format() all the time, even for debug messages to myself.
Nice way to solve that problem. I added some CSS-code to my wrapper, but now the .format() identified the curly brackets as a key that should be replaced. Is there a way to escape needed curly brackets?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.