爬虫――正则表达式(附加实例:爬取csdn首页内容)附:表达式全集()
正则表达式手册地址: http://tool.oschina.net/uploads/apidocs/jquery/regexp.html 小点: re: 用于提取字符串内容的模块。 爬虫过程:1> 创建正则对象;2> 匹配查找;3> 提取数据保存; 爬虫三个步骤:1.向页面发送请求,获取源代码(都是静态页面的代码);2.利用正则匹配数据;3.保存到数据库中; re.S 可以将正则的搜索域不再是一行,而是整个html字符串。 # .*? 非贪婪匹配 findall() 返回是列表 从响应对象中获取源代码字符串。 string = "a1234booooobccccccb" # a.*?b a1234b # a.*b a1234booooobccccccb # 查找单个数据:search(): 不考虑数据的位置。 match(): 必须保证数据在目标字符串的起始位置。 string = '2a3' print(re.match(re.compile('(\d)'), string)) import re string = """ <html> <div><a href='www.baidu.com'></a></div></div></div> </div><title>正则</title></div> <html><div><a href='www.baidu1.com'></a>