V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jacklong
V2EX  ›  Python

Requests, 真正的为人类着想

  •  
  •   jacklong ·
    jack-long · 2015-01-26 21:45:21 +08:00 · 5545 次点击
    这是一个创建于 3349 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API 太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。

    在Python的世界里,事情不应该这么麻烦。

    From http://cn.python-requests.org/zh_CN/latest/

    20 条回复    2015-01-29 13:20:34 +08:00
    a2z
        1
    a2z  
       2015-01-26 22:02:58 +08:00 via iPhone
    一直在用…
    fredchen
        2
    fredchen  
       2015-01-26 22:54:09 +08:00
    爬虫利器
    fy
        3
    fy  
       2015-01-26 23:31:16 +08:00   ❤️ 1
    欢迎回到火星基地,致远星风光可好?
    ehs2013
        4
    ehs2013  
       2015-01-27 00:59:58 +08:00
    Python 的 REST 库不用 requests 还能用什么……
    ryd994
        5
    ryd994  
       2015-01-27 02:01:31 +08:00
    urllib3也相当好用,对大多数需要足够了
    est
        6
    est  
       2015-01-27 08:52:53 +08:00
    Ruby的 Net::HTTP 才是真渣。RestClient也是渣。
    cdlxkill
        7
    cdlxkill  
       2015-01-27 09:05:33 +08:00 via iPhone
    正好需要,mark一记
    Delbert
        8
    Delbert  
       2015-01-27 09:14:02 +08:00
    requests没法urlencode和decode啊
    yueyoum
        9
    yueyoum  
       2015-01-27 10:25:14 +08:00
    @Delbert

    为何不能?
    举个例子?
    Delbert
        10
    Delbert  
       2015-01-27 11:46:47 +08:00
    对python2,urllib.quote()和urllib.unquote()
    对python3,urllib.parse.quote()和urllin.parse.unquote()

    urlencode就是将空格变为%20,将/变为%2F的东西。
    urldecode的作用相反。

    请问requests应该怎么实现?我反正没找到。
    9hills
        11
    9hills  
       2015-01-27 12:20:07 +08:00
    @Delbert requests 本质上不用去做quote。。用params你随便写即可,内带了quote
    Delbert
        12
    Delbert  
       2015-01-27 12:41:49 +08:00
    @9hills 不是参数,我要urldecode,但是requests没有。我保存的结果全是乱码。
    Delbert
        13
    Delbert  
       2015-01-27 12:42:37 +08:00
    @yueyoum 见你楼下。回复的时候忘记提醒了。
    yakczh
        14
    yakczh  
       2015-01-27 13:40:36 +08:00
    请求gbk页面返回
    UnicodeEncodeError: 'gbk' codec can't encode character '\ufeff' in position 0: illegal multibyte sequence
    recall704
        15
    recall704  
       2015-01-27 14:17:51 +08:00
    我有个程序,开始用的,后来去掉了。
    tidewind
        16
    tidewind  
       2015-01-27 16:20:52 +08:00
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-

    import requests
    import urllib

    url = 'http://zh.wikipedia.org/wiki/'+"猪"

    print urllib.quote(url)
    print requests.utils.quote(url)
    rurl = requests.utils.quote(url)

    print urllib.unquote(rurl)
    print requests.utils.unquote(rurl)
    assassinpig
        17
    assassinpig  
       2015-01-27 21:49:01 +08:00
    准备学习使用了,目前还是在urllib2中
    lujiajing1126
        18
    lujiajing1126  
       2015-01-27 23:13:06 +08:00
    httpie就是基于requests的~赞
    GeekGao
        19
    GeekGao  
       2015-01-28 11:24:26 +08:00
    去邮件列表里吐槽吧,让它变成standard library
    rikeinei
        20
    rikeinei  
       2015-01-29 13:20:34 +08:00
    这个确实不错
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   954 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 20:49 · PVG 04:49 · LAX 13:49 · JFK 16:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.