新人求指教~~糗事百科爬虫~~~ - V2EX
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
RaymondchanGE
V2EX    Python

新人求指教~~糗事百科爬虫~~~

  •  
  •   RaymondchanGE 2015-07-21 17:26:03 +08:00 2923 次点击
    这是一个创建于 3759 天前的主题,其中的信息可能已经有所发展或是发生改变。

    coding:utf-8

    import urllib2
    import urllib
    import re
    import thread
    import time

    class SpiderModel:
    def __init
    _(self):
    self.page = 1
    self.pages = []
    self.enable = False

    def GetPage(self,page): myUrl = "http://qiushibaike.com/hot/page/" + page #'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'下面这个是我在网页源码看的 user_agent = 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/43.0.2357.134 Safari/537.36' headers = {'User-Agent': user_agent} req = urllib2.Request(myUrl,headers = headers) myRespOnse= urllib2.urlopen(req) myPage = myResponse.read() unicodePage = myPage.decode('utf-8') 

    <div class="content">

    一次,出去吃饭,一朋友说:“哥们,看啊,我妹来了,帮我接一下吧”我靠,正点啊,我刷下子出门了,这时一美女扭着就走向了我们,我还以为真是朋友他妹呢,就笑着出门迎接,搂着就说:“美女这间”,结果直接给了我一巴掌,“你谁啊”,我当时就蒙了,顿时明白了,他妈的,内哥们阴我啊~~~

    <!--2015-07-21 11:38:09-->

    </div> 源代码中内容部分

    myItems = re.findall('<div class="content">(.*?)</div>',unicodePage,re.S) items = [] for item in myItems: items.appen([item[0].replace("\n","")]) return items def LoadPage(self): while self.enable: if len(self.pages) < 2: try: myPage = self.GetPage(str(self.pagg)) self.page += 1 self.pages.append(myPage) except: print '无法加载' else: time.sleep(1) def ShowPage(self,nowPage,page): for items in nowPage: print u'第%d页' %page, items[0], items[1] myInput = raw_input() if myInput =="quit": self.enable = False break def Start(self): self.enable = True page = self.page print u'正在加载。。' thread.start_new_thread(self.LoadPage()) while self.enable: if self.pages: nowPage = self.pages[0] del self.pages[0] self.ShowPage(nowPage, page) page += 1 

    print u"""

    糗事百科 

    """

    print u'按下回车键浏览内容:'
    raw_input(' ')
    myModel = Spider_Model()
    myModel.Start()

    代码如上。编译器没有报错。然后按enter键显示无法加载,不知道错误出在哪了
    新人刚刚开始学python,在这请教各位了。麻烦了~~~

    rrrkren
        1
    rrrkren  
       2015-07-22 04:03:51 +08:00
    WKPlus
        2
    WKPlus  
       2015-07-22 12:47:30 +08:00   1
    self.GetPage(str(self.pagg)) 这里应该是self.page吧

    另外,遇到问题可以把try except去掉,看看异常提示
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1507 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 16:38 PVG 00:38 LAX 08:38 JFK 11:38
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86