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 · 2343 次点击
    这是一个创建于 3209 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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   ·   我们的愿景   ·   实用小工具   ·   2183 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.