标签: python-requests

使用请求通过http下载文件时进度条

我需要下载一个相当大的(~200MB)文件.我想出了如何在这里下载和保存文件.有一个进度条可以知道下载了多少,这将是一件好事.我找到了ProgressBar,但我不确定如何将两者合并在一起.

这是我尝试过的代码,但它没有用.

bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)
with closing(download_file()) as r:
    for i in range(20):
        bar.update(i)
Run Code Online (Sandbox Code Playgroud)

python python-requests

26
推荐指数
4
解决办法
2万
查看次数

'模块对象没有属性'get'Python错误请求?

我刚刚使用'easy_insatll'安装了Requests模块,我试图运行这个tutrorial的演示代码,

import requests
payload = {'username': 'xxxx', 'password': 'xxxxx'}
r = requests.get('https://github.com/timeline.json')
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:AttributeError: easy_install

任何的想法?谢谢

python error-handling python-requests

25
推荐指数
4
解决办法
5万
查看次数

Python请求 - 异常类型:ConnectionError - try:except不起作用

我使用web服务来检索一些数据,但有时网址不起作用,我的网站没有加载.你知道我如何处理以下异常,所以如果webservice不工作,网站没有问题吗?

Django Version: 1.3.1 
Exception Type: ConnectionError
Exception Value: 
HTTPConnectionPool(host='test.com', port=8580): Max retries exceeded with url:
Run Code Online (Sandbox Code Playgroud)

我用了

try:
   r = requests.get("http://test.com", timeout=0.001)
except requests.exceptions.RequestException as e:    # This is the correct syntax
   print e
   sys.exit(1)
Run Code Online (Sandbox Code Playgroud)

但没有任何反应

python connection url python-2.x python-requests

25
推荐指数
1
解决办法
5万
查看次数

如何使用Python请求获取pdf文件名?

我正在使用Python 请求lib从Web获取PDF文件.这工作正常,但我现在也想要原始文件名.如果我在Firefox中转到PDF文件并单击download它已经定义了一个文件名以保存pdf.我如何获得此文件名?

例如:

import requests
r = requests.get('http://www.researchgate.net/profile/M_Gotic/publication/260197848_Mater_Sci_Eng_B47_%281997%29_33/links/0c9605301e48beda0f000000.pdf')
print r.headers['content-type']  # prints 'application/pdf'
Run Code Online (Sandbox Code Playgroud)

我查了一下r.headers有趣的东西,但那里没有文件名.我实际上希望有类似的东西r.filename......

有谁知道如何获取带有请求库的下载PDF文件的文件名?

python pdf filenames python-requests

25
推荐指数
5
解决办法
2万
查看次数

Python请求获取('连接中止.',BadStatusLine("''",))错误

def download_torrent(url):
    fname = os.getcwd() + '/' + url.split('title=')[-1] + '.torrent'
    try:
        schema = ('http:')
        r = requests.get(schema + url, stream=True)
        with open(fname, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
                    f.flush()
    except requests.exceptions.RequestException as e:
        print('\n' + OutColors.LR + str(e))
        sys.exit(1)

    return fname
Run Code Online (Sandbox Code Playgroud)

在那段代码中,当我运行完整的脚本时,我收到一个错误.当我去实际下载洪流时,我得到:

('Connection aborted.', BadStatusLine("''",))
Run Code Online (Sandbox Code Playgroud)

我只发布了我认为与上述相关的代码块.整个脚本如下.这是来自pantuts,但我认为它不再维持,我试图让它运行python3.根据我的研究,错误可能意味着我使用的是http而不是https,但我已经尝试了两种方法.

原始剧本

python python-3.x python-requests

25
推荐指数
1
解决办法
4万
查看次数

在Google App Engine中使用Requests python库

我正在尝试在Google App Engine上使用令人敬畏的Requests库.我找到了urllib3的补丁,它请求依赖,与App Engine兼容.https://github.com/shazow/urllib3/issues/61

我能成功

import requests
Run Code Online (Sandbox Code Playgroud)

但是之后

response = requests.get('someurl')
Run Code Online (Sandbox Code Playgroud)

以下回溯失败.这是怎么回事?

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/admin/__init__.py", line 317, in post
    exec(compiled_code, globals())
  File "<string>", line 6, in <module>
  File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/api.py", line 52, in get
    return request('get', url, **kwargs)
  File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/api.py", line 40, in request
    return s.request(method=method, url=url, **kwargs)
  File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/sessions.py", line 208, in request
    r.send(prefetch=prefetch)
  File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/models.py", line 458, in send
    self.auth = get_netrc_auth(url)
  File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/utils.py", line 43, in get_netrc_auth
    for loc in locations:
  File "/Users/Rohan/Dropbox/MuktiTechnologiesINC/MuktiTechnologies/GAE/humanmictweet/GAE/libraries/requests/utils.py", …
Run Code Online (Sandbox Code Playgroud)

google-app-engine urllib3 python-requests

24
推荐指数
3
解决办法
1万
查看次数

如何在python请求中使用相同的键发布多个值?

requests.post(url, data={'interests':'football','interests':'basketball'})
Run Code Online (Sandbox Code Playgroud)

我尝试过这个,但它没有用.我将如何张贴football,并basketball在interests现场?

python python-requests

24
推荐指数
2
解决办法
2万
查看次数

基本身份验证不与请求库一起使用

我正在尝试在python中使用基本身份验证

auth = requests.post('http://' + hostname, auth=HTTPBasicAuth(user, password))
request = requests.get('http://' + hostname + '/rest/applications')
Run Code Online (Sandbox Code Playgroud)

响应表单auth变量:

<<class 'requests.cookies.RequestsCookieJar'>[<Cookie JSESSIONID=cb10906c6219c07f887dff5312fb for appdynamics/controller>]>
200
CaseInsensitiveDict({'content-encoding': 'gzip', 'x-powered-by': 'JSP/2.2', 'transfer-encoding': 'chunked', 'set-cookie': 'JSESSIONID=cb10906c6219c07f887dff5312fb; Path=/controller; HttpOnly', 'expires': 'Wed, 05 Nov 2014 19:03:37 GMT', 'server': 'nginx/1.1.19', 'connection': 'keep-alive', 'pragma': 'no-cache', 'cache-control': 'max-age=78000', 'date': 'Tue, 04 Nov 2014 21:23:37 GMT', 'content-type': 'text/html;charset=ISO-8859-1'})
Run Code Online (Sandbox Code Playgroud)

但是当我试图从不同的位置获取数据时, - 我有401错误

<<class 'requests.cookies.RequestsCookieJar'>[]>
401
CaseInsensitiveDict({'content-length': '1073', 'x-powered-by': 'Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)', 'expires': 'Thu, 01 …
Run Code Online (Sandbox Code Playgroud)

python api python-requests appdynamics

24
推荐指数
4
解决办法
5万
查看次数

Python SSL连接"EOF违反协议"

我正在使用Django Celery任务连接到使用Gevent请求lib的Facebook Graph API.我一直在运行的问题是,我偶尔会发生违反协议异常的EOF.我已经四处搜索,各种来源提供了不同的修复,但似乎都没有.

我试过猴子修补ssl模块(gevent.monkey.patch_all())和其他一些但没有运气.

我甚至不确定这是否是openssl问题,因为一些消息来源可能会建议我在应用Gevent优化之前没有遇到过它

Connection error: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol
Traceback (most recent call last):
File "/home/user/workspace/startup/project/events/tasks.py", line 52, in _process_page
data = requests.get(current_url)
File "/home/user/workspace/startup/env/local/lib/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/home/user/workspace/startup/env/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/home/user/workspace/startup/env/local/lib/python2.7/site-packages/requests/sessions.py", line 354, in request
resp = self.send(prep, **send_kwargs)
File "/home/user/workspace/startup/env/local/lib/python2.7/site-packages/requests/sessions.py", line 460, in send
r = adapter.send(request, **kwargs)
File "/home/user/workspace/startup/env/local/lib/python2.7/site-packages/requests/adapters.py", line 250, in send
raise SSLError(e)
SSLError: …
Run Code Online (Sandbox Code Playgroud)

python django celery gevent python-requests

23
推荐指数
1
解决办法
5万
查看次数

Python中使用请求的查询字符串数组参数

我一直试图弄清楚如何使用python-requests发送网址的请求:

http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world'
Run Code Online (Sandbox Code Playgroud)

通常我可以构建一个字典并执行:

data = {'name': 'hello', 'data': 'world'}
response = requests.get('http://example.com/api/add.json', params=data)
Run Code Online (Sandbox Code Playgroud)

这适用于我所做的大多数事情.但是,我从上面打了url结构,我不知道如何在没有手动构建字符串的情况下在python中执行此操作.我可以做到,但不愿意.

在我丢失的请求库中有什么东西或者我不知道的一些python功能吗?

你甚至称那种类型的参数,所以我可以更好地谷歌吗?

python python-requests

23
推荐指数
3
解决办法
3万
查看次数