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
cdwyd
V2EX  ›  Python

python requests 无法获取 youtube 的内容

  •  
  •   cdwyd · 2015-01-07 12:45:09 +08:00 · 3312 次点击
    这是一个创建于 3407 天前的主题,其中的信息可能已经有所发展或是发生改变。
    RT,代码如下:

    import requests

    session = requests.session()
    session.proxies = {'http': '127.0.0.1:8123'}
    timeout = 20
    url = 'http://www.youtube.com'

    html = session.get(url, timeout = timeout).text

    之前一直用的好好的,找了半天也没找到原因,为什么就获取不到了
    第 1 条附言  ·  2015-01-07 14:15:25 +08:00
    错误信息:
    (<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x01EF4EB0>, 'Connection to www.youtube.com tim
    ed out. (connect timeout=20)')
    第 2 条附言  ·  2015-01-07 14:28:40 +08:00
    刚又继续测试了下,http的都能打开,https的都无法打开
    youtube是强制使用https的吗?
    11 条回复    2015-01-07 15:29:15 +08:00
    ultimate010
        1
    ultimate010  
       2015-01-07 13:28:14 +08:00   ❤️ 1
    本地开的http代理能用吗?
    cdwyd
        2
    cdwyd  
    OP
       2015-01-07 13:30:56 +08:00
    @ultimate010
    能用,访问别的都是正常的
    ultimate010
        4
    ultimate010  
       2015-01-07 13:49:29 +08:00
    你把youtube.com改成其他网站,试一试,如果可以,就是你的代理不能访问youtube.com
    iewgnaw
        5
    iewgnaw  
       2015-01-07 13:56:35 +08:00
    错误信息都不贴出来吗
    binux
        6
    binux  
       2015-01-07 13:59:18 +08:00
    什么叫:无法获取?获取不到?
    cdwyd
        7
    cdwyd  
    OP
       2015-01-07 14:11:12 +08:00
    @binux
    运行后没反应,一段时间后提示timeout
    Sylv
        8
    Sylv  
       2015-01-07 14:35:07 +08:00
    >>> import requests
    >>> r = requests.get('http://www.youtube.com')
    >>> r.url
    u'https://www.youtube.com/'
    >>> r.history
    [<Response [301]>]
    >>> r.history[0].headers['location']
    'https://www.youtube.com/'
    ryd994
        9
    ryd994  
       2015-01-07 14:36:28 +08:00
    @cdwyd timeout这是典型被墙啊
    yegle
        10
    yegle  
       2015-01-07 14:59:49 +08:00 via Android
    因为你设置的是HTTP的代理服务器,youtube首页会301跳转到HTTPS。
    thinker3
        11
    thinker3  
       2015-01-07 15:29:15 +08:00
    @yegle 貌似正确
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   971 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:40 · PVG 04:40 · LAX 13:40 · JFK 16:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.