在urllib2和POST调用上有很多东西,但我遇到了问题.
我正在尝试对服务进行简单的POST调用:
url = 'http://myserver/post_service'
data = urllib.urlencode({'name' : 'joe',
'age' : '10'})
content = urllib2.urlopen(url=url, data=data).read()
print content
Run Code Online (Sandbox Code Playgroud)
我可以看到服务器日志,当我将数据参数发送到urlopen时,它说我正在进行GET调用.
该库引发了404错误(未找到),这对于GET调用是正确的,POST调用处理得很好(我也尝试使用HTML表单中的POST).
我使用以下代码将大型文件从Internet流式传输到本地文件:
fp = open(file, 'wb')
req = urllib2.urlopen(url)
for line in req:
fp.write(line)
fp.close()
Run Code Online (Sandbox Code Playgroud)
这有效,但下载速度很慢.有更快的方法吗?(文件很大,所以我不想把它们留在内存中.)
尝试urlopen维基百科的某个页面时,我有一个奇怪的错误.这是页面:
http://en.wikipedia.org/wiki/OpenCola_(drink)
这是shell会话:
>>> f = urllib2.urlopen('http://en.wikipedia.org/wiki/OpenCola_(drink)')
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 4.0\src\debug\tserver\_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "c:\Python26\Lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "c:\Python26\Lib\urllib2.py", line 397, in open
response = meth(req, response)
File "c:\Python26\Lib\urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "c:\Python26\Lib\urllib2.py", line 435, in error
return self._call_chain(*args)
File "c:\Python26\Lib\urllib2.py", line 369, in _call_chain
result = …Run Code Online (Sandbox Code Playgroud) 我试图通过在Python中编写登录序列脚本来测试Web应用程序的功能,但我遇到了一些麻烦.
这是我需要做的事情:
现在,我对python相对较新,但到目前为止我测试过的两件事都没有用.首先我使用了httplib,putrequest()(传递URL中的参数)和putheader().这似乎没有遵循重定向.
然后我尝试了urllib和urllib2,将标题和参数作为dicts传递.这似乎返回登录页面,而不是我尝试登录的页面,我想这是因为缺少cookie或其他东西.
我错过了一些简单的事吗?
谢谢.
我想certification validation在使用内部企业链接向服务器发出请求时忽略它.
使用python requests库我会这样做:
r = requests.get(link, allow_redirects=False,verify=False)
Run Code Online (Sandbox Code Playgroud)
如何使用urllib2库进行相同的操作?
如何使用带urllib2的SOCKS 4/5代理下载网页?
我目前正在尝试使用Python登录网站,但该网站似乎在同一页面上发送cookie和重定向语句.Python似乎遵循该重定向,从而阻止我阅读登录页面发送的cookie.如何防止Python的urllib(或urllib2)urlopen跟随重定向?
问题:使用Python的urllib2发布数据时,所有数据都经过URL编码并作为Content-Type发送:application/x-www-form-urlencoded.上传文件时,应将Content-Type设置为multipart/form-data,并将内容编码为MIME.这个问题的讨论在这里:http: //code.activestate.com/recipes/146306/
为了解决这个限制,一些敏锐的程序员创建了一个名为MultipartPostHandler的库,它创建了一个OpenerDirector,您可以使用urllib2来主要使用multipart/form-data自动POST.此库的副本位于:http: //peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html
我是Python的新手,无法让这个库工作.我基本上写了下面的代码.当我在本地HTTP代理中捕获它时,我可以看到数据仍然是URL编码的,而不是多部分MIME编码.请帮我弄清楚我做错了什么或更好的方法来完成这件事.谢谢 :-)
FROM_ADDR = 'my@email.com'
try:
data = open(file, 'rb').read()
except:
print "Error: could not open file %s for reading" % file
print "Check permissions on the file or folder it resides in"
sys.exit(1)
# Build the POST request
url = "http://somedomain.com/?action=analyze"
post_data = {}
post_data['analysisType'] = 'file'
post_data['executable'] = data
post_data['notification'] = 'email'
post_data['email'] = FROM_ADDR
# MIME encode the POST payload
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
urllib2.install_opener(opener)
request = urllib2.Request(url, post_data)
request.set_proxy('127.0.0.1:8080', …Run Code Online (Sandbox Code Playgroud) 我需要存储可以使用任何语言的网站内容.我需要能够在内容中搜索Unicode字符串.
我尝试过类似的东西:
import urllib2
req = urllib2.urlopen('http://lenta.ru')
content = req.read()
Run Code Online (Sandbox Code Playgroud)
内容是一个字节流,所以我可以在其中搜索Unicode字符串.
我需要一些方法,当我这样做urlopen,然后阅读使用标题中的charset解码内容并将其编码为UTF-8.
虽然从移植代码python2到3,我从一个URL读取时出现此错误
TypeError:initial_value必须是str或None,而不是字节.
import urllib
import json
import gzip
from urllib.parse import urlencode
from urllib.request import Request
service_url = 'https://babelfy.io/v1/disambiguate'
text = 'BabelNet is both a multilingual encyclopedic dictionary and a semantic network'
lang = 'EN'
Key = 'KEY'
params = {
'text' : text,
'key' : Key,
'lang' :'EN'
}
url = service_url + '?' + urllib.urlencode(params)
request = Request(url)
request.add_header('Accept-encoding', 'gzip')
response = urllib.request.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO(response.read())
f = gzip.GzipFile(fileobj=buf)
data …Run Code Online (Sandbox Code Playgroud)