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

陷在这段思路里了,求指教

  •  
  •   omg21 · Sep 27, 2016 · 3246 views
    This topic created in 3531 days ago, the information mentioned may be changed or developed.
    现在有一批网页要处理,都是不同站点的。我用 BS4 做的处理代码能处理大部分的页面,有一小部分页面需要单独设计代码才能取得需要的数据。
    如果把全部站点用一段代码来处理,做一个循环,每一个都处理一遍,那一小部分需要单独处理的页面就需要用 if 判断来确定。很显然,这部分需要单独处理的页面每个都需要一个 if 来判断,这就大大增加了程序运行时间,因为每一个页面都要经过那若干 if 判断后才能处理。
    如果把单独处理页面的代码单拿出来,就等于用了两套类似的代码来做相同的事件,改动代码时也很麻烦,需要两处同时修改。
    不知道还有什么方法能够实现在一段代码里高效的处理上述问题的解决方案。
    5 replies    2016-10-08 22:13:12 +08:00
    264768502
        1
    264768502  
       Sep 27, 2016 via iPad
    EAFP LBYL
    menc
        2
    menc  
       Sep 28, 2016 via iPhone
    用 dict 来存,查找时间度降低为 O(1),可以视为不占用资源了
    Carrycat
        3
    Carrycat  
       Sep 28, 2016
    用 if else 判罚吧, if 后 python 不会 evaluate else 的东西,整个 runtime 还是 O ( n) 重写一遍就是 O ( n+m )了,别担心 python 判断 if 的速度。还有就是有句话说的好 "Any fool can write code that a computer can understand... But only good programmers write code that humans can understand." -- Martin Fowler
    linruiyi
        4
    linruiyi  
       Oct 8, 2016
    一个思路:做一个数据库,作为给各个独立设置代码的网站的配置数据库,写成一个通用的网页处理工具。这样子当单独设计的网页多的时候,后期只需要给数据库增加配置,当然你还可以搞个前端页面来进行配置写入。
    omg21
        5
    omg21  
    OP
       Oct 8, 2016
    @linruiyi 这样做就等于是运行时从两个表中取数据,前一个表存放的是通用的页面,直接取出来就处理就可以了,后一个表存放需单独处理的页面,挨个判断进行处理,可是这就需要用两套代码了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2958 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 07:41 · PVG 15:41 · LAX 00:41 · JFK 03:41
    ♥ Do have faith in what you're doing.