python - 我无法使用从网络上获取的数据
问题描述
我正在从网络上获取数据,但我不能像 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"])
解决方案
你得到 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'])
推荐阅读
- javascript - 将表单序列化到 localStorage / 在页面重新加载后恢复它(没有 jQuery)
- python - 如何同时在 CNN 和 Gist 数据上训练 Keras 模型?
- windows - Win10:Cmd批处理文件到Powershell的转换
- react-native - 无法使用带有 React Native 项目的 react-apollo-hooks 使 Mutation 工作
- reactjs - 反应状态值不会更新
- c - 大小为 8 的无效读取。尝试制作 2D 数组
- ios - 通过 UITextFields 约束问题交换
- python - 如何重命名python蝗虫动作?
- ios - 各种与 Apple 相关的包管理器的相对市场份额是多少?
- python - 我想了解以下程序的递归逻辑以确定如何计算输出