zoukankan      html  css  js  c++  java
  • 团队-爬虫豆瓣top250项目-项目进度

     

    正则表达式在线检测工具:http://tool.oschina.net/regex/

    进程:

    1.源代码HTML

      #将url转换为HTML源码
    def getHtml(url):
        try:
            page = urllib.request.urlopen(url)
            html = page.read()
        except:
            print("failed to geturl")
            return ''
        else:
            return html

    2.爬取书名

      #通过正则表达式获取该网页下每本书的title(换行符没去掉)
    def getTitle(html):
        nameList = re.findall(r'<a href="https.*?".*?target="_blank">(.*?)</a>',html,re.S)
        newNameList = [];
        global topnum
        for index,item in enumerate(nameList):
            if item.find("img") == -1:#通过检测img,只保留中文标题
                #item.replace(' ','')
                #item.strip()
                #item.splitlines()
                #re.sub(' | ', '', item)
                if topnum%26 !=0:
                    #newNameList.append("Top " + str(topnum) + " " + item);
                    newNameList.append(item);
                topnum += 1;
        return newNameList

    3.爬取图片

      #通过正则表达式获取该网页下每本书的图片链接
    def getImg(html):
        imgList = re.findall(r'img.*?width=.*?src="(http.*?)"',html,re.S)
        newImgList = []
        for index,item in enumerate(imgList):
            if item.find("js") == -and item.find("css") == -and item.find("dale") == -and item.find("icon") == -1and item.find("png") == -1:
                newImgList.append(item);

        return newImgList;

    4.翻页

      #实现翻页,每页25个
    for page in range(0,450,25):
        url = "https://www.douban.com/doulist/1264675/?start={}".format(page)
        html = getHtml(url).decode("UTF-8");
        if html == '':
            namesUrl.extend('none');
            imgsUrl.extend('none')
            scoresUrl.extend('none')
            commentsUrl.extend('none')
            introductionsUrl.extend('none')
        else:
            namesUrl.extend(getTitle(html))
            imgsUrl.extend(getImg(html))
            scoresUrl.extend(getScore(html))
            commentsUrl.extend(getComment(html))
            introductionsUrl.extend(getDetail(html))

    暂时完成以上的模块

    遇到的问题:

    1.通过观察爬取的结果,发现每一页都会多出一个内容(并不是我需要的数据,确符合正则表达式,所以通过简单的处理将其剔除掉)。这项有个小瑕疵:爬取的标题前后带着换行符,试了几种方法还是没去掉!!!

    2.因为页面中符合条件的数据各式各样,所以需要将其中不是我们需要的剔除掉(判断条件有点暴力,暂时没想到更好的办法)

  • 相关阅读:
    16位汇编第三讲 分段存储管理思想
    16位汇编语言第二讲系统调用原理,以及各个寄存器详解
    /bin/sh 与 /bin/bash 的区别
    Linux中cat、more、less、tail、head命令的区别
    Linux之特殊权限(SUID/SGID/SBIT)
    HTML页面参数的传递与获取
    Ajax的跨域请求——JSONP的使用
    IDEA新建maven项目
    IDEA新建Web项目
    权限管理基础——原理与解决方案
  • 原文地址:https://www.cnblogs.com/npqnpq/p/7691865.html
Copyright © 2011-2022 走看看