使用 urllib 模块从 url 中获取详细信息。
import urllib
url = "http://www.omdbapi.com/?t=UN%20HOMME%20ID%C3%89AL"
res = urllib.urlopen(url)
print res.code
data = res.read()
通过
json 模块
解析数据为 JSON。
import json
data1 = json.loads(data)
使用 xlwt 模块创建 xls 文件。
data = {"Title":"Un homme idéal","Year":"2015","Rated":"N/A",\
"Released":"18 Mar 2015","Runtime":"97 min","Genre":"Thriller",\
"Director":"Yann Gozlan","Writer":"Yann Gozlan, Guillaume Lemans, Grégoire Vigneron",\
"Actors":"Pierre Niney, Ana Girardot, André Marcon, Valeria Cavalli",\
"Plot":"N/A","Language":"French","Country":"France","Awards":"N/A",\
"Poster":"N/A","Metascore":"N/A","imdbRating":"6.3","imdbVotes":"214",\
"imdbID":"tt4058500","Type":"movie","Response":"True"}
import xlwt
book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("AssetsReport0")
colunm_count = 0
for title, value in data.iteritems():
sheet1.write(0, colunm_count, title)
sheet1.write(1, colunm_count, value)
colunm_count += 1
file_name = "test.xls"%()
book.save(file_name)
从用户那里获取 URL。
-
通过命令行参数:
使用sys.argv 获取从命令传递的参数。
演示:
import sys
print "Arguments:", sys.argv
输出:
vivek:~/workspace/vtestproject/study$ python polydict.py arg1 arg2 arg3
Arguments: ['polydict.py', 'arg1', 'arg2', 'arg3']
- 通过 Raw_input() /input() 方法
演示:
>>> url = raw_input("Enter url:-")
Enter url:-www.google.com
>>> url
'www.google.com'
>>>
注意:
在 Python 2.x 中使用 raw_input()
使用 Python 3.x 的输入