首页 > 解决方案 > 我无法使用从网络上获取的数据

问题描述

我正在从网络上获取数据,但我不能像 json 或字典那样使用它。

import requests
from bs4 import BeautifulSoup

url = "http://www.omdbapi.com/?apikey=73a4d84d&t=Tenet"
response = requests.get(url)
content = response.content
soup = BeautifulSoup(content,"html.parser")

print(soup["Title"])

标签: pythonjsondictionarybeautifulsouprequest

解决方案


你得到 JSON 响应。您可以使用方便Response.json()的方法对其进行反序列化。

import requests

url = "http://www.omdbapi.com/?apikey=73a4d84d&t=Tenet"
response = requests.get(url)
data = response.json() # same as data = json.loads(response.text) after import json
print(data)
print(data['Title'])

推荐阅读