尝试使用python生成,以使用cgi进行筛选。但是,当我从命令行运行它时,我总是得到一个错误,指出它在'print "Content-type:text/html\r\n\r\n“行中缺少括号。非常感谢您的帮助。谢谢。
#!/usr/bin/python3
import urllib.request
import json
import os
link = "https://api.nasa.gov/planetary/apod?api_key....."
resp = urllib.request.urlopen(link)
data = resp.read()
print(str(data, 'utf-8'))
returnJson = json.loads(data)
img_url = returnJson['url']
title = returnJson['title']
current_date = returnJson['date']
(filename, headers) = urllib.request.urlretrieve(img_url)
img_file_name = img_url.split('/')[-1]
os.rename(filename, img_file_name)
html = """
<center>
       <h1>Astronomy Picture of the Day</h1>
       <img src="%s">
       <p><b>%s</b></p>
</center>
""" % (img_file_name, title)
html_file_name = 'nasa_apod_%s.html' %current_date
print "Content-type:text/html\r\n\r\n" **Where it says parenthesis**
print '<html>'
print '<head>'
print '<title>Astronomy Picture of the Day</title>'
print '</head>'
print '<body>'
print '<h1>Astronomy Picture of the Day</h1>'
print '</body>'
print '</html>'发布于 2021-04-26 23:06:11
这是因为您使用的是Python3。在Python3中,print是一个函数,而不是一个语句。因此,这意味着您需要在打印的内容两边添加括号。
# this will fail in Python 3
print "Content-type:text/html\r\n\r\n"
# but this will work
print("Content-type:text/html\r\n\r\n")正如您之前所做的那样,使用print(str(data, 'utf-8'))
https://stackoverflow.com/questions/67268841
复制相似问题