【问题标题】:Timeout during fetching https website using Python使用 Python 获取 https 网站时超时
【发布时间】:2017-09-20 11:43:52
【问题描述】:

我无法使用 Python 和 requests 库获取 zomato.com 网站。

import requests
r = requests.get('https://www.zomato.com/san-antonio')
print r.status_code

我运行这个脚本并没有得到任何响应。我猜问题出在 https,但我在其他一些 https 网站上尝试过,效果很好,控制台上打印了 200。

我错过了什么吗?

【问题讨论】:

    标签: python python-2.7 python-requests


    【解决方案1】:

    你需要假装你来自一个真正的浏览器:

    import requests
    r = requests.get('https://www.zomato.com/san-antonio', headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"})
    
    print(r.status_code)
    
    # returns: 200
    

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 2020-03-19
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      相关资源
      最近更新 更多