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

[小白求助] 爬取猫眼 top100,打印显示为空

  •  
  •   15874103329 · 2018-12-03 21:45:31 +08:00 · 2091 次点击
    这是一个创建于 1942 天前的主题,其中的信息可能已经有所发展或是发生改变。
    源代码如下:

    import requests
    from requests.exceptions import RequestException

    def get_one_page(url):
    try:
    response = requests.get(url)
    if requests.status_codes == 200:
    return response.text
    return None
    except RequestException:
    return None

    def main():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36'}
    url = requests.get('https://www.hao123.com/', headers=headers)
    html = get_one_page(url)
    print(html)

    if __name__ == '__main__':
    main()
    6 条回复    2018-12-06 08:54:24 +08:00
    F1024
        1
    F1024  
       2018-12-03 21:59:54 +08:00
    网址都错了啊 兄弟
    hly9469
        2
    hly9469  
       2018-12-03 22:07:53 +08:00 via iPhone
    @F1024 #1 哈哈哈哈哈哈
    deepdark
        3
    deepdark  
       2018-12-03 22:11:40 +08:00 via Android
    @F1024 哈哈哈哈哈哈哈哈哈 hao123 还行
    lanqing
        4
    lanqing  
       2018-12-04 08:48:13 +08:00
    找点教程看看吧
    15874103329
        5
    15874103329  
    OP
       2018-12-04 10:00:24 +08:00
    额,网址之前是对的,后来换别的网址也是这样,不知道哪里出问题了
    zh584728
        6
    zh584728  
       2018-12-06 08:54:24 +08:00
    requests.get 请求了两遍,第二遍肯定是 return None 啦
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1008 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:46 · PVG 03:46 · LAX 12:46 · JFK 15:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.