Python httplib2,属性错误:“设置”对象没有属性“items”;

发布于 2024-10-28 12:21:00 字数 2186 浏览 2 评论 0原文

我正在使用 Python 库 httplib2。以下是我的代码。

import urllib.parse
import httplib2

httplib2.debuglevel = 1

http = httplib2.Http()

url = "http://login.sina.com.cn/hd/signin.php"
body = {"act": "1",
        "entry": "vblog",
        "password": "P@$sW0rd",
        "reference": "http://vupload.you.video.sina.com.cn/u.php?m=1&cate=0",
        "reg_entry": "vblog",
        "remLoginName": "on",
        "username": "this_is_user_name",
        "x": "",
        "y": ""}

headers = {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
           "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
           "Accept-Encoding", "gzip,deflate",
           "Accept-Language", "en-us,en;q=0.5",
           "Connection", "keep-alive",
           "Content-Length", "181",
           "Content-Type", "application/x-www-form-urlencoded",
           "Host", "login.sina.com.cn",
           "Keep-Alive", "115",
           "Referer", "http://login.sina.com.cn/hd/signin.php?entry=vblog&r=http%3A%2F%2Fvupload.you.video.sina.com.cn%2Fu.php%3Fm%3D1%26cate%3D0",
           "User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16"}

response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))

当我执行它时,我收到错误:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>python --version
Python 3.2

C:\>python a.py
Traceback (most recent call last):
  File "a.py", line 32, in <module>
    response, content = http.request(url, 'POST', urllib.parse.urlencode(body), headers=headers)
  File "C:\Python32\lib\site-packages\httplib2\__init__.py", line 961, in request
    headers = _normalize_headers(headers)
  File "C:\Python32\lib\site-packages\httplib2\__init__.py", line 184, in _normalize_headers
    return dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip())  for (key, value) in headers.items()])
AttributeError: 'set' object has no attribute 'items'

C:\>

我对 Python 和 httplib2 很陌生,我的代码有什么问题吗?

I'm playing with the Python library httplib2. The following is my code.

import urllib.parse
import httplib2

httplib2.debuglevel = 1

http = httplib2.Http()

url = "http://login.sina.com.cn/hd/signin.php"
body = {"act": "1",
        "entry": "vblog",
        "password": "P@$sW0rd",
        "reference": "http://vupload.you.video.sina.com.cn/u.php?m=1&cate=0",
        "reg_entry": "vblog",
        "remLoginName": "on",
        "username": "this_is_user_name",
        "x": "",
        "y": ""}

headers = {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
           "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
           "Accept-Encoding", "gzip,deflate",
           "Accept-Language", "en-us,en;q=0.5",
           "Connection", "keep-alive",
           "Content-Length", "181",
           "Content-Type", "application/x-www-form-urlencoded",
           "Host", "login.sina.com.cn",
           "Keep-Alive", "115",
           "Referer", "http://login.sina.com.cn/hd/signin.php?entry=vblog&r=http%3A%2F%2Fvupload.you.video.sina.com.cn%2Fu.php%3Fm%3D1%26cate%3D0",
           "User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16"}

response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))

When I execute it, I get the error:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>python --version
Python 3.2

C:\>python a.py
Traceback (most recent call last):
  File "a.py", line 32, in <module>
    response, content = http.request(url, 'POST', urllib.parse.urlencode(body), headers=headers)
  File "C:\Python32\lib\site-packages\httplib2\__init__.py", line 961, in request
    headers = _normalize_headers(headers)
  File "C:\Python32\lib\site-packages\httplib2\__init__.py", line 184, in _normalize_headers
    return dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip())  for (key, value) in headers.items()])
AttributeError: 'set' object has no attribute 'items'

C:\>

I'm pretty new to Python and httplib2, anything wrong in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘紸啶 2024-11-04 12:21:00

headers 应该是字典,而不是集合:

headers = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
           ...}

注意冒号而不是逗号。

headers should be a dictionary, not a set:

headers = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
           ...}

Note the colon instead of the comma.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文