网站建设与运营推广的回报材料移动网站虚拟主机
一. 前言
在前面的几篇文章中我介绍了如何通过Python分析源代码来爬取博客、维基百科InfoBox和图片,其文章链接如下:
其中核心代码如下:
# coding=utf-8  
import urllib  
import re  
  
#下载静态HTML网页  
url='http://www.csdn.net/'  
content = urllib.urlopen(url).read()  
open('csdn.html','w+').write(content)  
#获取标题  
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  
#获取超链接内容   
href = r'<a href=.*?>(.*?)</a>'  
m = re.findall(href,content,re.S|re.M)  
for text in m:  
    print unicode(text,'utf-8')  
    break #只输出一个url  