记录python正则学习中遇到的问题,以供日后参考。
例如,使用python正则爬取freebuf最新内容title和URL
思路:查看源代码,发现所有最新内容title和url都在 “news-info”和”news-img”这两个类中,通过一次性定位到这两个类中的内容来进行爬取。
难点主要是不会构造正则表达式,经过学习可写出来,但是较为繁琐。
#coding=utf-8
import re
import requests
from distutils.filelist import findall
contents= requests.get('http://www.freebuf.com/').text
pattern=re.compile('<div class="news-img.