Python爬取网页信息时,经常使用的正则表达式及方法。
1.获取<tr></tr>标签之间内容2.获取<a href..></a>超链接之间内容3.获取URL最后一个参数命名图片或传递参数4.爬取网页中所有URL链接5.爬取网页标题title两种方法6.定位table位置并爬取属性-属性值7.过滤<span></span>等标签8.获取<script></script>等标签内容
1). 获取<tr></tr>标签之间内容
开始标签如:<tr>、<th>、<td>、<a>、<table>、<div>...
后缀标签如:</tr>、</th>、</td>、</a>、</table>、</div>...
核心代码:
res_tr = r'<tr>(.*?)</tr>'m_tr = re.findall(res_tr,language,re.S|re.M)
# eg_v1
import relanguage = '''<tr><th>床前明月光</th><td>忧思独伤心</td></tr><tr>'''# 正则表达式获取<tr></tr>之间内容res_tr = r"<tr>(.*?)</tr>"m_tr = re.findall(res_tr,language,re.S|re.M)print (unicode(m_tr,"utf-8"))for line in m_tr:print lineres_th = r"<th>(.*?)</th>"m_th = re.findall(res_th,line,re.S|re.M)for mm in m_th:print (unicode(mm,"utf-8"))res_td = r"<td>(.*?)</td>"m_td = re.findall(res_td,line,re.S|re.M)for nn in m_td:print (unicode(nn,"utf-8"))
2). 获取超链接<a href=..></a>之间内容
在使用正则表达式时,需要分析网页链接,获取URL或网页内容。核心代码如下:
res = r'<a .*?>(.*?)</a>'mm = re.findall(res, content, re.S|re.M)urls=re.findall(r"<a.*?href=.*?<\/a>", content, re.I|re.S|re.M)
# eg_v2
import recontent = '''<td><a href="/articles/zj.html" title="浙江省">浙江省主题介绍</a><a href="//articles/gz.html" title="贵州省">贵州省主题介绍</a></td>'''
# 获取<a href></a>之间的内容
res = r'<a .*?>(.*?)</a>'mm = re.findall(res,content,re.S|re.M)for value in mm:print (value)
# 获取所有<a href></a>链接所有内容
urls = re.findall(r"a.*?href=.*?<\/a>",content,re.I|re.S|re.M)for i in urls:print i
# 获取<a href></a>中的URL
res_url = r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')"link = re.findall(res_url,content,re.I|re.S|re.M)for url in link:print (url)
3). 获取URL最后一个参数命名图片或传递参数
使用Python爬取图片过程中,会遇到图片对应的URL最后一个字段通常用于命名图片
通过该URL的"/"后面的参数命名图片
# eg_v3
urls = '/blogfile/11/11/BbsImg141568417848931_640*640.jpg'value = urls.split("/")[-1]print (value)# BbsImg141568417848931_640*640.jpg
使用Python获取GET方法的URL链接中,还可能存在传递参数的值,获取参数方法:
url = 'http://localhost/test.py?a=hello&b=world'values = url.split('?')[-1]print (values)# a=hello&b=worldfor key_value in values.split('&'):print (key_value.split('='))# ['a', 'hello']# ['b', 'world']
4). 爬取网页中所有URL链接
从固有网页中爬取URL链接,再进行下一步的循环爬取或URL抓取.
# eg_v4 (爬取CSDN首页的所有URL链接)
import reimport urllib2url = "/"content = urllib2.urlopen(url).read()urls = re.findall(r'<a.*?href=.*?<\/a>',content,re.I)for url in urls:print (unicode(url,"utf-8"))link_list = re.findall(r"(?=href=\".+?(?=\")|(?<=href=\').+?(?=\')",content)for url in link_list:print (url)
5). 爬取网页标题title两种方法
获取网页标题也是一种常见的爬虫,常位于<html><head><title>标题</title></head></html>中
# eg_v5 (爬取CSDN标题)
import reimport urllib2url = '/'content = urllib2.urlopen(url).read()# 方法一title_pat = r'(?<=<title>).*?(?=</title>)'title_ex = pile(title_pat,re.M|re.S)title_obj = re.search(title_ex,content)title = title_obj.group()print (title)# 方法二title = re.findall(r'<title>(.*?)</title>',content)print (title[0])
6). 定位table位置并爬取属性-属性值
# eg_v6 (正则表达式获取td值的例子)
import res = '''<table> <tr> <td>序列号</td><td>DEIN3-39CD3-2093J3</td> <td>日期</td><td>1月22日</td> <td>售价</td><td>392.70 元</td> <td>说明</td><td>仅限5用户使用</td> </tr> </table>'''res = r'<td>(.*?)</td><td>(.*?)</td>'m = re.findall(res,s,re.S|re.M)for line in m:print (unicode(line[0],'utf-8'),unicode(line[1],'utf-8'))
# 如果<td id="">包含该属性则正则表达式为r'<td id=.*?>(.*?)</td>';同样如果不一定是id属性开头,则可以使用正则表达式r'<td .*?>(.*?)</td>'
7). 过滤<span></span>等标签
获取值过程中,通常会存在<span>、<br>、<a href>等标签
核心代码:
elif "span" in nn: #处理标签<span>res_value = r'<span .*?>(.*?)</span>'m_value = re.findall(res_value,nn,re.S|re.M) for value in m_value:print unicode(value,'utf-8'),
# eg_v7
import relanguage = '''<table class="infobox bordered vcard" style="width: 21em; font-size: 89%; text-align: left;" cellpadding="3"><caption style="text-align: center; font-size: larger;" class="fn"><b>购书</b></caption><tr><th>性別:</th><td>男</td>d</tr><tr><th>異名:</th><td><span class="nickname">(字) 翔宇</span></td></tr><tr><th>政黨:</th><td><span class="org"><a href="../articles/%E4%B8%AD9A.html" title="购书中心"></a></span></td></tr><tr><th>籍貫:</th><td><a href="../articles/%E6%B5%9981.html" title="广东省">广东省</a><a href="../articles/%E7%BB%8D82.html" title="绍兴市">绍兴市</a></td></tr></table>''' # 获取table中tr值res_tr = r'<tr>(.*?)</tr>'m_tr = re.findall(res_tr,language,re.S|re.M)# 获取表格第一列th 属性for line in m_tr:res_th = r'<th>(.*?)</th>'m_th = re.findall(res_th,line,re.S|re.M)for mm in m_th:if "href" in mm: # 如果获取加粗的th中含超链接则处理restr = r'<a href=.*?(.*?)<\a>'h = re.findall(res_tr,mm,re.S|re.M)print (unicode(h[0],'utf-8'))else:print (unicode(mm,'utf-8'))# 取表格第二列td 属性值res_td = r'<td>(.*?)</td>'m_td = re.findall(res_td,line,re.S|re.M)for nn in m_td:if 'href' in nn:res_value = r'<a .*?>(.*?)</a>'m_value = re.findall(res_value,nn,re.S|re.M)for value in m_value:print (unicode(value,'utf-8'))elif "span" in nn:res_value = r'<span .*?>(.*?)</span>'m_value = re.findall(res_value,nn,re.S|re.M)for value in m_value:print (unicode(value,'utf-8')else:print (unicode(nn,'utf-8'))print (' ')
8). 获取<script></script>等标签内容
在获取游讯网图库中,图集对应的原图它是存储在script中,其中获取原图-original即可
通过正则表达式下载URL:
res_original = r'"original":"(.*?)"' #原图m_original = re.findall(res_original,script)
# eg_v8
import reimport osimport urllibcontent = '''<script>var images = [ { "big":"http://i-//3/18/KDkwMHgp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg", "thumb":"http://i-//3/18/KHgxMjAp/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg", "original":"http://i-//3/18/6381ccc0-ed65-4422-8671-b3158d6ad23e.jpg", "title":"","descript":"","id":75109}, { "big":"http://i-//3/18/KDkwMHgp/fec26de9-8727-424a-b272-f2827669a320.jpg", "thumb":"http://i-//3/18/KHgxMjAp/fec26de9-8727-424a-b272-f2827669a320.jpg", "original":"http://i-//3/18/fec26de9-8727-424a-b272-f2827669a320.jpg", "title":"","descript":"","id":75110}, </script> '''html_script = r'<script>(.*?)</script>'m_script = re.findall(html_script,content,re.S|re.M)for script in m_script:res_original = r'"original":"(.*?)"'m_original = re.findall(res_original,script)for pic_url in m_original:print (pic_url)filename = os.path.basename(pic_url)urllib.urlretrieve(pic_url,"D:\\" + filename)