0

I have a list of links and some text stored in database. I wish to print the text using the corresponding links using <a href.

In a for loop, I tried to print the anchors this way :

for i in range(8):
    #print url_list[i], question[i]
    print "<a href=\"" + url_list[i] + "\">" + question[i] + "<\/a>" + "<br>"

The commented part prints the text but the second gives a blank page. This should print 8 links on the page.

What could be wrong ?

4

1 回答 1

0

What could be wrong?

For one thing, "<\/a>" should be "</a>".

For another, your python is incredibly difficult to read. Try:

print r'<a href="{0}">{1}</a><br>'.format(url_list[i], question[i])

or even:

for url, q in zip(url_list, question):
  print r'<a href="{0}">{1}</a><br>'.format(url, q)

Also note that if your URLs have special characters in them (such as space or ampersand), you will need to escape them:

import urllib
for url, q in zip(url_list, question):
  url = urllib.quote_plus(url)
  print r'<a href="{0}">{1}</a><br>'.format(url, q)
于 2013-03-05T21:33:23.023 回答