python-爬虫实战

发布时间:2017-12-12 00:11:36编辑:Run阅读(3705)

    想看看最近的电影院播放的今日影视,就以这个网址https://movie.douban.com/people/1166776/为例,先使用urllib.request模块抓取整个网页,再使用re模块获取影视信息

    编写simpleCrawlerNowMoive.py代码如下

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    import re
    import urllib.request
    class TodayMoive(object):
        def __init__(self):
            self.url = 'https://movie.douban.com/people/1166776/'
            self.timeout = 3
            self.filename = 'todaymoive.txt'
            '''内部变量定义完毕'''
            self.getmoiveinfo()
    
        def getmoiveinfo(self):
            response = urllib.request.urlopen(self.url, timeout=self.timeout)
            content = response.read().decode('utf-8')
            #findall匹配电影名字的段落
            moivelist = re.findall('class="cover"><img alt="', '')
            st = st.replace('"', '')
            #split字符串切割,以' '空格为分隔符,取第0个值
            st = st.split(' ')[0]
            return st
    
    if __name__ == '__main__':
        tm = TodayMoive()

    应该看到的结果blob.png

    会在当前目录下生成一个todaymoive.txt文件,内容如下

    blob.png

    看起来不像是网络爬虫,对吗?严格来说这个就是网络爬虫了,只是爬取的内容很简单,也很少,当爬取的内容比较少的时候,网络爬虫也可以这么写,稍微复杂点的,爬取内容多一点的,按照这个方法写那就很痛苦了,这个时候就要用到爬虫框架了

关键字

上一篇: python-其它有用模块1

下一篇: python-sys模块