Skip to main content
 首页 » 编程设计

python之在没有外部模块的网站上查找信息

2025年05月04日14davidwang456

我正在用 Python 创建一个程序,您可以在其中搜索电视节目/电影,并从 IMDb 中获得:

电影的标题、年份、分级、年龄分级和概要。

我想完全不使用外部模块,只使用 Python 3.4 附带的模块。

我知道我将不得不使用 urllib,但我不知道从那里去哪里。

我该怎么做?

请您参考如下方法:

这是取自 from here 的例子:

import json 
from urllib.parse import quote 
from urllib.request import urlopen 
 
def search(title): 
    API_URL = "http://www.omdbapi.com/?r=json&s=%s" 
    title = title.encode("utf-8") 
    url = API_URL % quote(title) 
    data = urlopen(url).read().decode("utf-8") 
    data = json.loads(data) 
    if data.get("Response") == "False": 
        print(data.get("Error", "Unknown error")) 
 
    return data.get("Search", []) 

然后你可以这样做:

>>> search("Idiocracy") 
[{'Year': '2006', 'imdbID': 'tt0387808', 'Title': 'Idiocracy'}]