从 omdbapi.movie_search 导入 GetMovie
def get_movie_data(api_key, 标题): 电影数据 = {} movie_api = GetMovie(api_key=api_key)
for title in titles:
# Attempt to fetch the movie information
try:
result = movie_api.get_movie(title=title)
# Check if movie details were found and the title matches the queried title
if result and result.get('Response', 'False') == 'True' and result.get('Title') == title:
movie_data[result.get('Title')] = result
except GetMovieException as e:
# Handle "Movie not found" exception
print(f"Movie not found: {title}")
continue
except Exception as e:
# Handle other exceptions
print(f"Error querying title {title}: {e}")
continue
return movie_data
api_key = 'api-key'
titles = [x for x in movie_data['title'].head(10)] # 根据自己的实际数据调整
movies_info = get_movie_data(api_key, 标题)
打印(电影信息)
使用 omdb API 迭代电影列表以获取信息时遇到问题。之前有人使用过这个 API 吗?可以为我指明正确的方向吗?
我期待得到一本标题和电影信息的字典。但是,当我迭代电影列表时,我得到所有电影的相同信息。
我测试了 omdbapi 库,但是当我运行它时它不起作用。
直接通过 requests 库调用怎么样?
GET http://www.omdbapi.com/?t={movie title}&apikey={your API key}
详细信息在这里
我将获取三部电影并保存到 JSON 文件及其标题名称。
另存为
demo.py
import requests
import json
def get_movie(title, api_key):
base_url = 'http://www.omdbapi.com/'
# Replace spaces in the title with '+'
formatted_title = title.replace(' ', '+')
params = {
't': formatted_title,
'apikey': api_key
}
response = requests.get(base_url, params=params)
if response.status_code == 200:
return response.json()
else:
return {'error': 'Failed to fetch data', 'status_code': response.status_code}
def save_movie_data(title, data):
with open(f"{title}.json", "w") as f:
json.dump(data, f, indent=4)
api_key = 'your API key' # Replace with your actual API key
movie_titles = ['The Godfather', 'Interstellar', 'Toy Story']
for title in movie_titles:
movie_data = get_movie(title, api_key)
save_movie_data(title, movie_data)
print(f"Data for '{title}' saved to '{title}.json'")
python demo.py
已保存
Interstellar.json
{
"Title": "Interstellar",
"Year": "2014",
"Rated": "PG-13",
"Released": "07 Nov 2014",
"Runtime": "169 min",
"Genre": "Adventure, Drama, Sci-Fi",
"Director": "Christopher Nolan",
"Writer": "Jonathan Nolan, Christopher Nolan",
"Actors": "Matthew McConaughey, Anne Hathaway, Jessica Chastain",
"Plot": "When Earth becomes uninhabitable in the future, a farmer and ex-NASA pilot, Joseph Cooper, is tasked to pilot a spacecraft, along with a team of researchers, to find a new planet for humans.",
"Language": "English",
"Country": "United States, United Kingdom, Canada",
"Awards": "Won 1 Oscar. 44 wins & 148 nominations total",
"Poster": "https://m.media-amazon.com/images/M/MV5BZjdkOTU3MDktN2IxOS00OGEyLWFmMjktY2FiMmZkNWIyODZiXkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg",
"Ratings": [
{
"Source": "Internet Movie Database",
"Value": "8.7/10"
},
{
"Source": "Rotten Tomatoes",
"Value": "73%"
},
{
"Source": "Metacritic",
"Value": "74/100"
}
],
"Metascore": "74",
"imdbRating": "8.7",
"imdbVotes": "2,071,776",
"imdbID": "tt0816692",
"Type": "movie",
"DVD": "24 May 2016",
"BoxOffice": "$188,020,017",
"Production": "N/A",
"Website": "N/A",
"Response": "True"
}
还有其他两个 json 文件。