本文介绍了在python 3中用urllib打开一个url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从阳光基金会打开此 API 的 URL,并以 json 格式从页面返回数据.这是我生成的代码,减去 myapikey 周围的括号.

i'm trying to open the URL of this API from the sunlight foundation and return the data from the page in json. this is the code Ive produced, minus the parenthesis around myapikey.

import urllib.request.urlopen
import json

urllib.request.urlopen("https://sunlightlabs.github.io/congress/legislators?api_key='(myapikey)")

我收到这个错误

Traceback (most recent call last):
  File "<input>", line 1, in <module>
ImportError: No module named request.urlopen

我做错了什么?我研究了 https://docs.python.org/3/library/urllib.request.html 仍然没有进展

what am i doing wrong? ive researched into https://docs.python.org/3/library/urllib.request.html and still no progress

推荐答案

你需要使用 from urllib.request import urlopen,另外我建议你使用 with 语句打开连接时.

You need to use from urllib.request import urlopen, also I suggest you use the with statement while opening a connection.

from urllib.request import urlopen

with urlopen("https://sunlightlabs.github.io/congress/legislators?api_key='(myapikey)") as conn:
    # dosomething

这篇关于在python 3中用urllib打开一个url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:56
查看更多