V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
yazoox
V2EX  ›  Python

公司内部有个 https 的网站,我想用 Python 去拿它的内容,可是提示我 403......

  •  
  •   yazoox · Apr 5, 2018 · 6809 views
    This topic created in 2953 days ago, the information mentioned may be changed or developed.
    <html><head> <title>403 Forbidden</title> </head><body>

    Forbidden

    You don't have permission to access / on this server.

    </body></html>

    1.我直接用浏览器是可以正常打开浏览的(只要连接到公司内部的网络,就可以打开,不需要登录域帐号)

    2.然后我用 Postman 去 GET https://server_address 也能够成功 (我没有设置任何 header,只是填写了 server 地址,然后“发送”)

    3.接着,在 postman 里面,能够拿到发送的信息,然后,postman 有个功能,可以直接转换成代码。里面有个是 python 的,我就转换了。

    4.把转换后的代码,放到我的 python 脚本里面

    结果就是上面的这个 403 提示了。 这个是咋回事儿呢? 难道我需要提供 username/password ?可是我浏览这个网站时,没有这些啊?

    5.postman 还可以转换成 curl 的命令行( cur -X GET ......),我试了一下,居然也可以成功拿回 html 的完整内容。就是在 python 中不行。

    谢谢!

    Supplement 1  ·  Apr 6, 2018

    问题解决了,还是使用requests,在header里面添加了 user-agent,模拟Mozila 5.10 例如:

    headers = {
        'User-agent': "Mozilla 5.10",
        'cache-control': "no-cache",
        'postman-token': "220d2989-c111-fea3-874f-f5c31111db49"
    }
    
    14 replies    2018-04-06 23:27:16 +08:00
    hotsun168
        1
    hotsun168  
       Apr 5, 2018 via iPhone
    有没有可能是 useragent 识别。
    owlsec
        2
    owlsec  
       Apr 5, 2018
    贴代码吧。。。
    eluotao
        3
    eluotao  
       Apr 5, 2018 via iPhone
    postman 可以,那就是你代码问题了
    Molita
        4
    Molita  
       Apr 6, 2018 via Android
    agent 头 识别 把比如 requests 这类的 ban 了?
    heyf
        5
    heyf  
       Apr 6, 2018 via iPhone
    @hotsun168 应该是,豆瓣的 api 也是得加 useragent
    zhchyu999
        6
    zhchyu999  
       Apr 6, 2018 via Android
    抓包看结果
    shootsoft
        7
    shootsoft  
       Apr 6, 2018 via iPhone
    内网有的会看 cookie,建议问问 IT
    eastrd
        8
    eastrd  
       Apr 6, 2018 via Android
    建议你直接用 Selenium 模块来模拟浏览器访问网页,这样就不需要手动模拟各种 request headers 了。先从 chrome webdriver 开始,最后 phantomjs
    yazoox
        9
    yazoox  
    OP
       Apr 6, 2018 via Android
    @zhchyu999 兄弟
    你说的抓包是说用什么 fiddle,鲨鱼 shark 这类工具么?不会……
    hotsun168
        10
    hotsun168  
       Apr 6, 2018 via iPhone
    @yazoox 我很早之前有用过一个叫 smartsniff 的工具,抓包的,分别正常浏览器打开网页,和用你的代码打开网页,对比抓到的包内容的区别。
    ZoomQuiet
        11
    ZoomQuiet  
       Apr 6, 2018 via iPhone
    是也乎 ╮(╯▽╰)╭

    能用 cURL ?
    那就用 py 调 cURL 通过 pipe 获得数据啊啊啊啊啊啊啊啊…

    python 是胶水…不是白胶的~
    mianju
        12
    mianju  
       Apr 6, 2018
    ciensoledad
        13
    ciensoledad  
       Apr 6, 2018 via Android
    之前遇到过类似的 分析之后用了 requests_ntml 解决了登陆问题
    mingyun
        14
    mingyun  
       Apr 6, 2018
    @mianju 这个需要翻墙?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5659 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 369ms · UTC 07:41 · PVG 15:41 · LAX 00:41 · JFK 03:41
    ♥ Do have faith in what you're doing.