Which is best in Python: urllib2, PycURL or mechanize?

前端 未结 8 1318
一个人的身影
一个人的身影 2021-01-29 17:48

Ok so I need to download some web pages using Python and did a quick investigation of my options.

Included with Python:

urllib - seems to me that I should use ur

8条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-29 18:07

    To "get some webpages", use requests!

    From http://docs.python-requests.org/en/latest/ :

    Python’s standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken. It was built for a different time — and a different web. It requires an enormous amount of work (even method overrides) to perform the simplest of tasks.

    Things shouldn’t be this way. Not in Python.

    >>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
    >>> r.status_code
    200
    >>> r.headers['content-type']
    'application/json; charset=utf8'
    >>> r.encoding
    'utf-8'
    >>> r.text
    u'{"type":"User"...'
    >>> r.json()
    {u'private_gists': 419, u'total_private_repos': 77, ...}
    

提交回复
热议问题