【问题标题】:how to write output of a python script to html file如何将python脚本的输出写入html文件
【发布时间】:2014-09-02 03:27:51
【问题描述】:

我是 python 脚本的新手。问题是我想在 python 脚本中输入谷歌搜索查询并将响应写入本地 html 文件。我曾尝试为相同的内容编写 python 脚本。但不确定如何将输出定向到 html 文件。任何人都可以提供这个答案。谢谢。

下面是python脚本。

import urllib2
import urllib
import json

url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&"

query = raw_input("What do you want to search for ? >> ")

query = urllib.urlencode( {'q' : query } )

response = urllib2.urlopen (url + query ).read()

data = json.loads ( response )

results = data [ 'responseData' ] [ 'results' ]

for result in results:
    title = result['title']
    url = result['url']
    print ( title + '; ' + url )

【问题讨论】:

  • 我不确定我是否理解这个问题。 file = open("output.html", "w"); file.write(my_output); file.close() 怎么了?

标签: python html


【解决方案1】:

您可以为此使用 Python 的内置函数 open

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2016-03-18
    相关资源
    最近更新 更多