【问题标题】:how can i change from python2 http request code to python3 Requests [closed]我如何从 python2 http 请求代码更改为 python3 请求 [关闭]
【发布时间】:2012-07-29 14:42:48
【问题描述】:

我用python2.6写了如下代码,想改成python3.2和Requests:

import urllib, urllib2
import cookielib 
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
urllib2.install_opener(opener)

params = urllib.urlencode(dict(x = '0' , y = '0', url='login.php', password='1234',id='userid'))   
f = urllib2.urlopen('http://test.com/login_check.php', params)  
data = f.read()   
login_response = opener.open('http://aaa.com/bbs/confirm.php?')
htmlContent = login_response.read()   
print htmlContent   
response.close()

【问题讨论】:

  • .. I want change it to python3.2 and Requests -- 这不是一个坏主意 -- 去吧。通读docs.python-requests.org/en/latest/index.html 应该使您能够编写所需的函数调用。附带说明:您发布的代码将为response.close() 引发NameError(也许这应该是login_response.close()

标签: python python-3.x python-requests


【解决方案1】:

如果你看一下文档,很简单:http://docs.python-requests.org/en/latest/index.html

import requests

url = r'http://test.com/login_check.php'
parameters = {'x' : '0' ,
              'y' : '0', 
              'url':'login.php',
              'password':'1234',
              'id':'userid'}

login_response = requests.get(url, params=parameters)
htmlContent = login_response.text

【讨论】:

  • 好的,我看到了这个文档,但是同样的错误 = 是 SyntaxError: invalid syntax in 'x'='0' 我不明白谢谢
  • 再试一次,但要确保= 现在是:。确保您尝试了解正在发生的事情,而不是简单地复制和粘贴。
  • 不,我太累了。我有很多时间尝试。我选择python2.7!谢谢
  • @JefferyLim 是的,没问题。该代码也适用于 2.7。
  • /对不起,我错了,请给我一个小费!发送请求 x = 0 /谢谢你的好意/如果机械化可以支持 python 3。我不能尝试请求!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多