首页 > 其他 > 详细

使用正则做的一个爬虫小例子

时间:2018-11-19 13:16:56      阅读:143      评论:0      收藏:0      [点我收藏+]
from urllib.request import urlopen
import re

"""
爬虫,爬一个豆瓣网,把爬出来的存储到一个文件
"""


def get_request(url):
    """
    把网址变为源码,因为计算机只能读懂源码,然后吧源码变为utf-8的格式
    :param url:
    :return:
    """
    ret = urlopen(url)
    return ret.read().decode(‘utf-8‘)


def get_findall(s):
    """
    把源码的utf-8的正则显示出来,满足的就以列表打印出来
    :param s:
    :return:
    """
    ret = re.findall(
            ‘<div class="item">.*?<div class="pic">.*?<em.*?>(?P<id>\d.).*?<span class="title">.*?(?P<name>.*?)</span>‘
            ‘.*?<span class="rating_num".*?>(?P<arg>.*?)</span>.*?<span>(?P<cou>.*?)评价</span>‘, s, re.S)
    return ret


def get_ur(w):
    """
    调用函数。

    :param w:
    :return:
    """
    url = ‘https://movie.douban.com/top250?start=%s&filter=‘ % w
    ret = get_request(url)
    ret = get_findall(ret)
    return ret


con = 0
while con < 30:
    ret = get_ur(con)
    with open(‘电影‘, ‘w‘, encoding=‘utf-8‘) as f:
        if ret:
            try:
                a = tuple(ret)
                b = list(a)
                f.write(str(b))
            except Exception:
                print(‘hehe1‘)


    f.close()

print(ret)

 

使用正则做的一个爬虫小例子

原文:https://www.cnblogs.com/python884/p/9982548.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!