Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用httplib2进行编程登录时出现问题_Python_Httplib2 - Fatal编程技术网

Python 使用httplib2进行编程登录时出现问题

Python 使用httplib2进行编程登录时出现问题,python,httplib2,Python,Httplib2,我使用python中的httplib2库对网站进行编程登录。下面是我的代码: import urllib,httplib2 url='http://somesite.com' header_data={'Content-type': 'application/x-www-form-urlencoded', 'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.

我使用python中的httplib2库对网站进行编程登录。下面是我的代码:

import urllib,httplib2
url='http://somesite.com'
header_data={'Content-type': 'application/x-www-form-urlencoded',
    'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10'
}
body={'username':'<username>','password':'<password>','Submit':'Sign in'}
http = httplib2.Http()
try:
    response,content = http.request(url,'POST',headers=header_data,body=urllib.urlencode(body)) 
except:
    print False
print response
print content
导入urllib,httplib2
url='1〕http://somesite.com'
标题_data={'Content-type':'application/x-www-form-urlencoded',
“用户代理”:“Mozilla/5.0(X11;U;Linux i686;en-US;rv:1.9.2.10)Gecko/20100922 Ubuntu/10.10(maverick)Firefox/3.6.10”
}
正文={'username':'','password':'','Submit':'Sign-in'}
http=httplib2.http()
尝试:
响应,content=http.request(url,'POST',headers=header\u data,body=urllib.urlencode(body))
除:
打印错误
打印响应
印刷内容
我得到字典的响应,但内容变量为空。它应该有页面的html,不是吗

有什么办法解决这个问题吗? 请帮忙 谢谢。这个怎么样

from urllib import urlencode
from ulrlib2 import Request, urlopen

url='http://somesite.com'
header_data={'Content-type': 'application/x-www-form-urlencoded',
    'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10'}
body={'username':'<username>','password':'<password>','Submit':'Sign in'}

data = urlencode(body)
try:
    if method.upper() == 'GET':
        httpstream = urlopen(url+"&"+data)
    else:
        req = Request(url, data)
        httpstream = urlopen(req)
except:
    ...
result = httpstream.read()
httpstream.close()
从urllib导入urlencode
从ulrlib2导入请求,urlopen
url='1〕http://somesite.com'
标题_data={'Content-type':'application/x-www-form-urlencoded',
“用户代理”:“Mozilla/5.0(X11;U;Linux i686;en-US;rv:1.9.2.10)Gecko/20100922 Ubuntu/10.10(maverick)Firefox/3.6.10”
正文={'username':'','password':'','Submit':'Sign-in'}
数据=URL编码(正文)
尝试:
if method.upper()=“GET”:
httpstream=urlopen(url+“&”+数据)
其他:
req=请求(url、数据)
httpstream=urlopen(请求)
除:
...
结果=httpstream.read()
httpstream.close()

您应该向试图帮助您的用户提供一些反馈。