这是我正在尝试使用的模块,并且有一个我试图自动填充的表单.我想使用机械化请求的原因是因为使用Mechanize,我必须首先加载登录页面才能填写并提交,而使用请求,我可以跳过加载阶段直接发布消息(希望).基本上,我正在尝试使登录过程尽可能少地占用带宽.
我的第二个问题是,在登录过程和重定向之后,是否可能无法完全下载整个页面,而只能检索页面标题?基本上,单独的标题将告诉我登录是否成功,所以我想最小化带宽使用.
当涉及到HTTP请求等等时,我有点像菜鸟,所以任何帮助都会受到赞赏.仅供参考,这是一个学校项目.
编辑问题的第一部分已经回答.我现在的问题是第二部分
我目前正在编写一个从URL下载文件的脚本
import urllib.request
urllib.request.urlretrieve(my_url, 'my_filename')
Run Code Online (Sandbox Code Playgroud)
根据文档,urllib.request.urlretrieve是一个遗留的接口,可能会被弃用,因此我想避免它,所以我不必在不久的将来重写此代码.
我无法找到download(url, filename)标准库中的其他界面.如果urlretrieve被认为是Python 3中的遗留接口,那么替代品是什么?
我使用python请求(http://requests.readthedocs.org/en/latest/)编写了以下python脚本:
import requests
payload = {'key1': 'value 1', 'key2': 'value 2'}
headers = {'Content-Type': 'application/json;charset=UTF-8'}
r = requests.get("http://example.com/service", params=payload, headers=headers,
auth=("admin", "password"))
Run Code Online (Sandbox Code Playgroud)
如果我查看服务器的访问日志,传入的请求是:/ service?key1 = value ++ 1&key2 = value + 2
但是,服务器期望value%20%201&......
我已经读过使用+作为空格的占位符是内容类型application/x-www-form-urlencoded的一部分,但显然我已经请求了application/json.
有谁知道如何使用%20作为pythons请求的查询参数中的空格?
如果我从开发框中运行以下命令:
$ openssl s_client -connect github.com:443
Run Code Online (Sandbox Code Playgroud)
我得到以下最后一行输出:
Verify return code: 20 (unable to get local issuer certificate)
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用请求执行此操作,则会收到另一个失败请求:
>>> import requests
>>> r = requests.get('https://github.com/', verify=True)
Run Code Online (Sandbox Code Playgroud)
提出异常:
SSLError: [Errno 1] _ssl.c:507: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Run Code Online (Sandbox Code Playgroud)
我还可以使用verify标志运行第一个命令并获得类似的输出:
$ openssl s_client -connect github.com:443 -verify 9
...
Verify return code: 27 (certificate not trusted)
Run Code Online (Sandbox Code Playgroud)
基本上这告诉我证书有问题.我可以使用这两种方法指定一个特定的证书,它将起作用:
$ openssl s_client -connect github.com:443 -CAfile /etc/ssl/certs/DigiCert_High_Assurance_EV_Root_CA.pem -verify 9
...
Verify return code: 0 (ok)
Run Code Online (Sandbox Code Playgroud)
和:
>>> r = requests.get('https://github.com/', verify='/etc/ssl/certs/DigiCert...pem')
<Response [200]>
Run Code Online (Sandbox Code Playgroud)
那么,就我的问题而言,这究竟是什么问题?请求/ openssl不应该知道在哪里可以找到有效的证书吗?
其他信息:
我正在尝试教自己一些基本的网络抓取.使用Python的请求模块,我能够获取各种网站的HTML,直到我尝试这个:
>>> r = requests.get('http://www.wrcc.dri.edu/WRCCWrappers.py?sodxtrmts+028815+por+por+pcpn+none+mave+5+01+F')
Run Code Online (Sandbox Code Playgroud)
而不是作为此页面源的基本html,我得到:
>>> r.text
'\x1f\ufffd\x08\x00\x00\x00\x00\x00\x00\x03\ufffd]o\u06f8\x12\ufffd\ufffd\ufffd+\ufffd]...
>>> r.content
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed\x9d]o\xdb\xb8\x12\x86\xef\xfb+\x88]\x14h...
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多get/post的组合,我可以从文档和SO以及其他示例中猜出每种语法.我不明白我上面看到的是什么,无法把它变成我能读到的任何东西,也无法弄清楚如何得到我真正想要的东西.我的问题是,如何获取上述页面的html?
我正在使用微信API ...在这里我要使用这个API将图像上传到微信的服务器 http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files
url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=%s&type=image'%access_token
files = {
'file': (filename, open(filepath, 'rb'),
'Content-Type': 'image/jpeg',
'Content-Length': l
}
r = requests.post(url, files=files)
Run Code Online (Sandbox Code Playgroud)
我无法发布数据
想用wechat sdk创建菜单
WeChat.create_menu({
"button":[
{
"type":"click",
"name":"Daily Song",
"key":"V1001_TODAY_MUSIC"
},
{
"type":"click",
"name":" Artist Profile",
"key":"V1001_TODAY_SINGER"
},
{
"name":"Menu",
"sub_button":[
{
"type":"view",
"name":"Search",
"url":"http://www.soso.com/"
},
{
"type":"view",
"name":"Video",
"url":"http://v.qq.com/"
},
{
"type":"click",
"name":"Like us",
"key":"V1001_GOOD"
}]
}]
})
Run Code Online (Sandbox Code Playgroud)
由于此错误,目前无法正常工作:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, …Run Code Online (Sandbox Code Playgroud) 我试图运行python urllib2脚本并收到此错误:
InsecurePlatformWarning:真正的SSLContext对象不可用.这可以防止urllib3正确配置SSL,并可能导致某些SSL连接失败.有关更多信息,请参阅https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
谷歌搜索错误解决方案后,堆栈溢出是下载请求的安全包:
pip install requests[security]
但是当我跑步时,我得到错误......
zsh:找不到匹配项:请求[安全]
任何人都知道为什么zsh没有拿起这个库,它安装/升级请求就好了,我不知道为什么这不起作用
我在Debian服务器上运行它...
我正在尝试编写代码以登录网站。
首先,我使用ARC进行了测试。它工作正常。图片
所以我写了这样的python代码:
import requests
url = 'https://www.bible.ac.kr/login.aspx/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko',
'Referer': 'http://www.bible.ac.kr/'
}
req = requests.get(url, headers=headers) # OR requests.get(url, headers=headers, verify=False)
print(req.status_code)
Run Code Online (Sandbox Code Playgroud)
但我有错误。
错误
Traceback (most recent call last):
File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 603, in urlopen
chunked=chunked)
File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 344, in _make_request
self._validate_conn(conn)
File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 843, in _validate_conn
conn.connect()
File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/connection.py", line 370, in connect
ssl_context=context)
File "/Users/kyungmin/PycharmProjects/untitled14/venv/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 355, in ssl_wrap_socket
return context.wrap_socket(sock, …Run Code Online (Sandbox Code Playgroud) 我一直试图在两天内构建一个具有多线程功能的刮刀.不知怎的,我仍然无法管理它.起初我尝试了使用线程模块的常规多线程方法,但它并不比使用单个线程快.后来我才知道请求是阻塞的,多线程方法并没有真正起作用.所以我一直在研究并发现了关于grequest和gevent的事情.现在我正在使用gevent运行测试,它仍然不比使用单个线程更快.我的编码错了吗?
这是我班级的相关部分:
import gevent.monkey
from gevent.pool import Pool
import requests
gevent.monkey.patch_all()
class Test:
def __init__(self):
self.session = requests.Session()
self.pool = Pool(20)
self.urls = [...urls...]
def fetch(self, url):
try:
response = self.session.get(url, headers=self.headers)
except:
self.logger.error('Problem: ', id, exc_info=True)
self.doSomething(response)
def async(self):
for url in self.urls:
self.pool.spawn( self.fetch, url )
self.pool.join()
test = Test()
test.async()
Run Code Online (Sandbox Code Playgroud) python ×10
python-requests ×10
python-3.x ×2
ssl ×2
urllib ×2
wechat ×2
asynchronous ×1
curl ×1
forms ×1
gevent ×1
html ×1
openssl ×1
post ×1
python-2.7 ×1
security ×1