练练正则

前端之家收集整理的这篇文章主要介绍了练练正则前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!user/bin/python
# coding: utf-8
from bs4 import BeautifulSoup
import urllib
import re


def get_html(url):

    req = urllib.urlopen(url).read()
    return req

def handle_html(html):

    reg1 = re.compile(r'<span class="atc_title">.*?</span>',re.S)
    reg2 = re.compile(r'<span class="atc_title">\s*<a title=".*" target="_blank" href="(?P<link>.*)">(?P<title>.*)</a></span>')
    list1 = re.findall(reg1,html)
    list2 = []
    for title in list1:
        n = re.search(reg2,title)
        print n.group('title') + '\t' + n.group('link')
        list2.append(n.group('link'))
    return list2



def load_html(result):

    count = 0
    for link in result:
        count += 1
        urllib.urlretrieve(link,'D:\Documents\%s.html' % count)



if __name__ == '__main__':

    url = 'http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html'
    html = get_html(url)
    result = handle_html(html)
    load_html(result)





原文链接:https://www.f2er.com/regex/361996.html

猜你在找的正则表达式相关文章