一.正则表达式:
基本符号:
^ 表示匹配字符串的开始位置 (例外 用在中括号中[ ] 时,可以理解为取反,表示不匹配括号中字符串)
$ 表示匹配字符串的结束位置
* 表示匹配 零次到多次
+ 表示匹配 一次到多次 (至少有一次)
? 表示匹配零次或一次
. 表示匹配单个字符
| 表示为或者,两项中取一项
( ) 小括号表示匹配括号中全部字符
[ ] 中括号表示匹配括号中一个字符 范围描述 如[0-9 a-z A-Z]
{ } 大括号用于限定匹配次数 如 {n}表示匹配n个字符 {n,}表示至少匹配n个字符 {n,m}表示至少n,最多m
\ 转义字符 如上基本符号匹配都需要转义字符 如 \* 表示匹配*号
\w 表示英文字母和数字 \W 非字母和数字
\d 表示数字 \D 非数字
[\u4E00-\u9FA5] 连续汉字匹配
注:在【】中除【-】(范围)、【^】(非)、【\】(转义)这三种外,其余字符在[]内均无意义
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
常用的正则表达式(转)
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
匹配双字节字符(包括汉字在内):[^\x00-\xff]
匹配空行的正则表达式:\n[\s| ]*\r
匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/
匹配首尾空格的正则表达式:(^\s*)|(\s*$)
匹配IP地址的正则表达式:/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
匹配网址URL的正则表达式:http://(/[\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
sql语句:^(select|drop|delete|create|update|insert).*$
1、非负整数:^\d+$
2、正整数:^[0-9]*[1-9][0-9]*$
3、非正整数:^((-\d+)|(0+))$
4、负整数:^-[0-9]*[1-9][0-9]*$
5、整数:^-?\d+$
6、非负浮点数:^\d+(\.\d+)?$
7、正浮点数:^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
8、非正浮点数:^((-\d+\.\d+)?)|(0+(\.0+)?))$
9、负浮点数:^(-((正浮点数正则式)))$
10、英文字符串:^[A-Za-z]+$
11、英文大写串:^[A-Z]+$
12、英文小写串:^[a-z]+$
13、英文字符数字串:^[A-Za-z0-9]+$
14、英数字加下划线串:^\w+$
15、E-mail地址:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
16、URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$
或:^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$
17、邮政编码:^[1-9]\d{5}$
18、中文:^[\u0391-\uFFE5]+$
19、电话号码:^((\d2,3)|(\d{3}\-))?(0\d2,3|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$
20、手机号码:^((\d2,3)|(\d{3}\-))?13\d{9}$
21、双字节字符(包括汉字在内):^\x00-\xff
22、匹配首尾空格:(^\s*)|(\s*$)(像vbscript那样的trim函数)
23、匹配HTML标记:<(.*)>.*<\/\1>|<(.*) \/>
24、匹配空行:\n[\s| ]*\r
25、提取信息中的网络链接:(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
26、提取信息中的邮件地址:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
27、提取信息中的图片链接:(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
28、提取信息中的IP地址:(\d+)\.(\d+)\.(\d+)\.(\d+)
29、提取信息中的中国手机号码:(86)*0*13\d{9}
30、提取信息中的中国固定电话号码:(\d3,4|\d{3,4}-|\s)?\d{8}
31、提取信息中的中国电话号码(包括移动和固定电话):(\d3,4|\d{3,4}-|\s)?\d{7,14}
32、提取信息中的中国邮政编码:[1-9]{1}(\d+){5}
33、提取信息中的浮点数(即小数):(-?\d*)\.?\d+
34、提取信息中的任何数字 :(-?\d*)(\.\d+)?
35、IP:(\d+)\.(\d+)\.(\d+)\.(\d+)
36、电话区号:/^0\d{2,3}$/
37、腾讯QQ号:^[1-9]*[1-9][0-9]*$
38、帐号(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
39、中文、英文、数字及下划线:^[\u4e00-\u9fa5_a-zA-Z0-9]+$
*,+,{}都遵循贪婪原则,就是前面匹配到的不要,只要最后一次匹配到的 ,但是加了?就变成惰性匹配了(*?,+?)
# import re
# line='boooooooooooobbu123'
# a='.*(b.*b).*'
# b=re.match(a,line)
# print(b.group(1))#bb
# line='boooooooooooobbu123'
# a='.*(b.*).*'
# b=re.match(a,line)
# print(b.group(1)) #bu123
# line='aaaaaboooooooooooobbu123'
# a='.*?(b.*).*'
# b=re.match(a,line)
# print(b.group(1))#boooooooooooobbu123
# line='aaaaaboooooooooooobbu123'
# a='.*?(b.*b).*'
# b=re.match(a,line)
# print(b.group(1)) #boooooooooooobb
# line='18782902222 14782902222'
# a='(1[48357][0-9]{9}.*)' #[48357]4,8,3,5,7随意有一个就行 [0-9]范围区间
# b=re.match(a,line)
# print(b.group(1)) #18782902222 18782902222 14782902222
#
# line='study in 北京大学 上海大学'
# # a='.*?([\u4E00-\u9FA5].*)学'# 北京大学 上海大
# a='.*?([\u4E00-\u9FA5].*学)'#北京大学 上海大学
# b=re.match(a,line)
# print(b.group(1))
# line='boooooooooooobbu123'
# a='^b.*3$'
# b=re.match(a,line)
# print(b.group()) #boooooooooooobbu123
二。xpath
1.xpath术语
节点:<>这种包起来的
a.父节点 b.子节点 c.兄弟节点 (同级的) d.祖先节点(父亲节点及以上)
2.xpath语法
# re_selector=response.xpath( '/html/body/div[1]/div[3]/div[1]/div[1]/h1') #此时只是一个数组,用extract()[0]取值
# re1_selector=response.xpath('//*[@id="post-110287"]/div[1]/h1/text()')
# re1_selector = response.xpath('//*[@class="entry-header"]/h1/text()') #直接用这种方法
title= response.xpath('//*[@class="entry-header"]/h1/text()').extract()[0]
created_data=response.xpath('//p[@class="entry-meta-hide-on-mobile"]/text()').extract()[0].strip().replace('·','').strip()
dianzan=response.xpath('//span[contains(@class,"vote-post-up")]/h10/text()').extract()[0] #注意这里的contains内置函数的格式
dianzan1 = response.xpath('//h10[@id="110287votetotal"]/text()').extract()[0]
dianzan2 = response.xpath('//*[@id="110287votetotal"]/text()').extract()[0] #以上三个选取是一样的
shoucang_nums=response.xpath('//span[contains(@class,"bookmark-btn")]/text()').extract()[0]
match_re = re.match(".*?(\d+).*",shoucang_nums)
if match_re:
shoucang_nums=match_re.group(1)
zhengwen=response.xpath('//div[@class="entry"]').extract()[0]
taget_list = response.xpath('//p[@class="entry-meta-hide-on-mobile"]/a/text()').extract()
taget_list=[element for element in taget_list if not element.strip().endswith('评论')]
tags=','.join(taget_list)
CSS选择器:
规则看起来麻烦的一笔,但实际上css选择器却是我们选取元素最好的方法。
.entry-header h1::text 选取类entry-header 下h1的文本内容
p.entry-meta-hide-on-mobile::text 选择P标签类为entry-meta-hide-on-mobile的文本内1容
.类名1.类名1 选择类为类名1 和类名2
.类名1 .类名2 选择类为类名1下类为类名2(中间有空格)
title=response.css('.entry-header h1::text').extract_first("")
created_data=response.css('p.entry-meta-hide-on-mobile::text').extract()[0].strip().replace('·','').strip()
dianzan=response.css('.vote-post-up h10::text').extract()[0]
match_re2 = re.match(".*?(\d+).*", dianzan)
if match_re2:
dianzan = int(match_re2.group(1))
else:
dianzan = 0
shoucang_nums=response.css('span.bookmark-btn::text').extract()[0]
match_re = re.match(".*?(\d+).*", shoucang_nums)
if match_re:
shoucang_nums=int(match_re.group(1))
else:
shoucang_nums =0
pinglun_nums=response.css('a[href="#article-comment"] span::text').extract()[0]
match_re1 = re.match(".*?(\d+).*", pinglun_nums)
if match_re1:
pinglun_nums = int(match_re1.group(1))
else:
pinglun_nums = 0
context=response.css('div.entry').extract()[0].replace('\n','').replace('\r','').replace(' ','').replace('\t','').replace('\xa0','')
tags = response.css("p.entry-meta-hide-on-mobile a::text").extract()
tagdata = response.css("p.entry-meta-hide-on-mobile::text").extract()[0].strip()
tags = ",".join(tagdata)