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

为什么获取代码为空??

  •  
  •   happykjoy · 2018-09-20 09:54:10 +08:00 · 2603 次点击
    这是一个创建于 2016 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码: #!/usr/bin/env python

    -- coding:utf-8 --

    import requests import json

    def get_one_page(url,headers):

    response = requests.get(url,headers)
    print(response.cookies)
    print(response.status_code)
    print(response.text)
    print(type(response.text))
    print(response.json)
    print(type(response.json))
    

    if name == 'main': headers={ 'accept': 'text / html, application / xhtml + xml, application / xml;q = 0.9, image / webp, image / apng, * / *;q = 0.8', 'accept - encoding': 'gzip, deflate', 'accept - language': 'zh - CN', 'cache - control': 'max - age = 0', 'dnt': '1', 'upgrade - insecure - requests': '1', 'user - agent': 'Mozilla / 5.0(Windows NT 6.1) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 61.0.3163.79 Safari / 537.36 Maxthon / 5.2.3.3000', 'x - devtools - emulate - network - conditions - client - id': '0f286fdf - ae53 - 4784 - 9610 - 56f5b068a872' } url = 'https://www.toutiao.com/a6602782094278001159/' get_one_page(url,headers)

    运行结果: <RequestsCookieJar[]> 200

    <meta charset="UTF-8"><meta content="width=device-width,initial-scale=1" name="viewport"><meta content="ie=edge" http-equiv="X-UA-Compatible"><link href="//s3a.pstatp.com/toutiao/resource/ntoutiao_web/static/image/favicon_8e9c9c7.ico" rel="shortcut icon" type="image/x-icon"><title>今日头条</title>

    <class 'str'> <bound method Response.json of <Response [200]>> <class 'method'>

    为什么运行后代码为空,什么原因,网站屏蔽了吗???高手帮忙看一下。谢谢。

    4 条回复    2018-09-20 13:02:17 +08:00
    iSecret
        1
    iSecret  
       2018-09-20 10:23:37 +08:00
    爬今日头条需要用到 PhantomJS。
    NoString
        2
    NoString  
       2018-09-20 10:30:06 +08:00
    js 渲染的界面得先拿 phantomJS 运行完再..
    misaka19000
        3
    misaka19000  
       2018-09-20 11:29:46 +08:00
    请把代码格式化好再发
    happykjoy
        4
    happykjoy  
    OP
       2018-09-20 13:02:17 +08:00
    好的,谢谢各位回复。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5332 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:15 · PVG 17:15 · LAX 02:15 · JFK 05:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.