推荐学习书目
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
yuewolf
V2EX  ›  Python

第一个有点用的程序写出来了!激动中的疑惑

  •  1
     
  •   yuewolf · Mar 31, 2015 · 4392 views
    This topic created in 4104 days ago, the information mentioned may be changed or developed.
    求勿嘲!新手练习。实现功能很简单,就是看下新浪博客rss返回内容是否为空,不为空就把这个RSS地址存起来。
    求路过的大牛小牛们指点。
    问题
    1. rss 验证不为空时,一条条保存进txt好一点,还是我考虑的200一组写一次好一点?
    2. 如果有错或者异常咋办?能不能在先不学异常处理的前提下,直接绕过去?异常处理什么的,看教程有点发怵。
    3. 运行速度好慢,想抓完得跑到猴年马月…… 应该用多线程or多进程?
    4. WIN和LINUX下跑起来速度上有区别吗?
    5. 非常感谢!!
    代码如下:

    # -- coding: utf-8 --
    import re
    import sys
    import urllib2
    sys.setrecursionlimit(1000000)#不懂,什么深度来着的
    save_path = "blog.txt"#保存路径,先写死了
    def Main(start_num=2935650000,end_num=2935654000,per_num=200):
    print 'do you want to set ? \nYes (input 1 ) or No (input Enter)'#多么人性化的考虑啊
    select = raw_input('')
    if select == '1':
    start_num = int(raw_input('input start blog number : '))
    end_num = int(raw_input('input end blog number : '))
    per_num = int(raw_input('input per number : '))
    else:
    pass
    Do(start_num,end_num,per_num)
    print 'Done! Omg, how smart am I!'#自我表扬比自我批评更重要!
    def Do(start,end,per):
    temp_end = start + per
    the_str = ''
    if temp_end <= end:
    for i in range(start,temp_end):
    if BlogCheck(i):
    the_str = the_str + 'http://blog.sina.com.cn/rss/' + str(i) + '.xml\n'#这一坨有点别扭的感觉
    SaveResult(the_str)
    start = temp_end
    Do(start,end,per)#莫非我用上了传说中的递归??
    else:
    for i in range(start,end):
    if BlogCheck(i):
    the_str = the_str + str(i) + '\n'
    SaveResult(the_str)
    def BlogCheck(user):
    rss_url = 'http://blog.sina.com.cn/rss/' + str(user) + '.xml'
    response = urllib2.urlopen(rss_url)
    content = response.read()
    if content == '':#浪浪的blog未开通时 rss输出内容为空
    return False
    else:
    return True
    def SaveResult(str):
    f = open(save_path,'a')
    f.write(str)
    f.close()
    Main()
    Supplement 1  ·  Mar 31, 2015
    感谢@Pastsong 代码如下:
    15 replies    2015-03-31 19:35:27 +08:00
    xieyudi1990
        1
    xieyudi1990  
       Mar 31, 2015
    以前用纯C + socket实现过一个podcast自动下载器的路过...
    limbo0
        2
    limbo0  
       Mar 31, 2015
    C socket -> python爬虫 -> scrapy框架 -> 分布式爬虫 -> ....
    clino
        3
    clino  
       Mar 31, 2015
    python对多线程不友好,要用多进程或者协程
    fengchang
        4
    fengchang  
       Mar 31, 2015   ❤️ 1
    1.写成每条保存一次就行了。运行的时候系统会给你做缓冲区的。
    2.异常很简单的。外面套个try,然后except里忽略就行了。不用异常处理会变得更麻烦。
    3.用多线程
    4.区别不大,不过将来想挂在VPS上的话,还是得用Linux
    shyangs
        5
    shyangs  
       Mar 31, 2015   ❤️ 1
    python的排版都被吃掉了,你不会贴到gist吗?
    sujin190
        6
    sujin190  
       Mar 31, 2015
    @clino 其实如果网络io高的话也无所谓,下载的过程中是可以切换到另一个线程的
    sujin190
        7
    sujin190  
       Mar 31, 2015
    多线程吧,异常的话其实很简单的,你仔细看下它的介绍,一下就会了,python的错误处理很依赖于异常
    Kirscheis
        8
    Kirscheis  
       Mar 31, 2015 via iPhone
    python代码没有缩进几乎看不懂啊。。。
    只是需要在有异常的时候忽略跳过的话,用try…except…finally…就可以了
    tsingyi
        9
    tsingyi  
       Mar 31, 2015
    你确认程序真正正常执行结束过么?
    我发下你的递归是永远停不下了的哟~
    话说,为什么这里要用递归啦?
    yuewolf
        10
    yuewolf  
    OP
       Mar 31, 2015
    @tsingyi 正常结束了的
    没排版好的缘故吧。
    这个是递归吗?呃哈哈,看来真是啊。
    我开始的想法是:如果每条都读写一次,量大会影响硬盘,所以设置了个300一组。比如先判断 2000这个号到2300 这里有多少符合条件,然后一起写一次硬盘,看上面朋友的说法,应该是多虑了。
    yuewolf
        11
    yuewolf  
    OP
       Mar 31, 2015
    @shyangs gist 是什么?指github之类的吗?
    我新手,抱歉哈,不会贴代码
    Pastsong
        12
    Pastsong  
       Mar 31, 2015   ❤️ 1
    @yuewolf https://gist.github.com/ 适用于贴代码片段
    yuewolf
        13
    yuewolf  
    OP
       Mar 31, 2015
    @fengchang 非常感谢您的指导!
    yuewolf
        14
    yuewolf  
    OP
       Mar 31, 2015
    @Pastsong 哈哈 确实很好用,贴好了。
    kohnv
        15
    kohnv  
       Mar 31, 2015
    我觉得用argparse等命令解析的包, 直接在运行的时候指定你那些start_num等参数, 这样逼格高一点蛤蛤
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3717 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 00:45 · PVG 08:45 · LAX 17:45 · JFK 20:45
    ♥ Do have faith in what you're doing.