1、获取<tr></tr>、<div></div>
等标签之间内容 :
#coding=utf-8
import urllib
import urllib2
import re
html='<table border="1">\
<tr>\
<td>性别, 男</td>\
<td>row 1, cell 2</td>\
</tr>\
<tr>\
<td>row 2, cell 1</td>\
<td>row 2, cell 2</td>\
</tr>\
</table>'
p=r'<td>(.*?)</td>'
pattern1 = re.compile(p)
matcher1 = re.findall(pattern1,html)
for mat in matcher1:
print mat
2、获取url:
#coding=utf-8
import urllib
import urllib2
import re
url = "http://www.csdn.net/"
content = urllib.urlopen(url).read()
link_list = re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')", content)
for url in link_list:
print url
3、标题:
#coding=utf-8
import urllib
import urllib2
import re
url = "http://www.csdn.net/"
content = urllib.urlopen(url).read()
print u'方法一:'
title_pat = r'(?<=<title>).*?(?=</title>)'
title_ex = re.compile(title_pat, re.M | re.S)
title_obj = re.search(title_ex, content)
title = title_obj.group()
print title
print u'方法二:'
title = re.findall(r'<title>(.*?)</title>', content)
print title[0]
4、