【问题标题】:Python 3 Get HTTP pagePython 3 获取 HTTP 页面
【发布时间】:2011-01-02 16:37:42
【问题描述】:

如何让 python 获取 HTTP 页面的内容?到目前为止,我只有请求,并且已经导入了 http.client。

【问题讨论】:

    标签: python http python-3.x


    【解决方案1】:

    使用urllib.request 可能是最简单的方法:

    import urllib.request
    f = urllib.request.urlopen("http://stackoverflow.com")
    print(f.read())
    

    【讨论】:

    • 试过了,我得到了“AttributeError: 'module' object has no attribute 'urlopen'”
    • 抱歉,我刚刚注意到您使用的是 Python 3。我已经更新了我的示例以匹配。
    • @Davide Gualano:Python 2.x urllib2 模块已纳入 Python 3.x urllib 模块集:docs.python.org/library/urllib2.html
    • @Greg:我的错,我没有仔细阅读问题标题:)
    【解决方案2】:

    使用内置模块“http.client”

    import http.client
    
    connection = http.client.HTTPSConnection("api.bitbucket.org", timeout=2)
    connection.request('GET', '/2.0/repositories')
    response = connection.getresponse()
    print('{} {} - a response on a GET request by using "http.client"'.format(response.status, response.reason))
    content = response.read().decode('utf-8')
    print(content[:100], '...')
    

    结果:

    200 OK - 使用“http.client”响应 GET 请求 {“pagelen”:10,“值”:[{“scm”:“hg”,“网站”:“”,“has_wiki”: true, "name": "tweakmsg", "links ...

    使用第三方库“请求”

    response = requests.get("https://api.bitbucket.org/2.0/repositories")
    print('{} {} - a response on a GET request by using "requests"'.format(response.status_code, response.reason))
    content = response.content.decode('utf-8')
    print(content[:100], '...')
    

    结果:

    200 OK - 使用“请求”响应 GET 请求 {“pagelen”:10,“值”:[{“scm”:“hg”,“网站”:“”,“has_wiki”: true, "name": "tweakmsg", "links ...

    使用内置模块“urllib.request”

    response = urllib.request.urlopen("https://api.bitbucket.org/2.0/repositories")
    print('{} {} - a response on a GET request by using "urllib.request"'.format(response.status, response.reason))
    content = response.read().decode('utf-8')
    print(content[:100], '...')
    

    结果:

    200 OK - 使用“urllib.request”响应 GET 请求 {“pagelen”:10,“值”:[{“scm”:“hg”,“网站”:“”,“has_wiki”: true, "name": "tweakmsg", "links ...

    注意事项:

    1. Python 3.4
    2. 响应的结果很可能只是内容不同

    【讨论】:

      【解决方案3】:

      您还可以使用请求库。我发现这特别有用,因为它更容易检索和显示 HTTP 标头。

      import requests
      
      source = 'http://www.pythonlearn.com/code/intro-short.txt'
      
      r = requests.get(source)
      
      print('Display actual page\n')
      for line in r:
          print (line.strip())
      
      print('\nDisplay all headers\n')
      print(r.headers)
      

      【讨论】:

      • 这是 Python 3 吗?
      【解决方案4】:

      pip 安装请求

      import requests
      
      r = requests.get('https://api.spotify.com/v1/search?type=artist&q=beyonce')
      r.json()
      

      【讨论】:

        【解决方案5】:

        添加此代码可以格式化数据以供人类阅读:

        text = f.read().decode('utf-8')
        

        【讨论】:

          【解决方案6】:

          https://stackoverflow.com/a/41862742/8501970 而是检查一下。它与您遇到的问题大致相同,而且这个问题非常简单,代码行数很少。 当我意识到 python3 不能简单地使用 get_page 时,这肯定对我有所帮助。

          这是一个不错的选择。 (希望这会有所帮助,干杯!)

          【讨论】:

            猜你喜欢
            • 2014-12-15
            • 2017-11-17
            • 1970-01-01
            • 2021-02-23
            • 1970-01-01
            • 2010-11-28
            • 1970-01-01
            • 1970-01-01
            • 2020-07-17
            相关资源
            最近更新 更多