发布于 2017-07-20 11:17:02 | 223 次阅读 | 评论: 0 | 来源: 网友投递
这里有新鲜出炉的Python3 Cookbook中文版,程序狗速度看过来!
Python编程语言
Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。
本文实例讲述了Python正则抓取新闻标题和链接的方法。分享给大家供大家参考,具体如下:
#-*-coding:utf-8-*-
import re
from urllib import urlretrieve
from urllib import urlopen
#获取网页信息
doc = urlopen("http://www.itongji.cn/news/").read() #自己找的一个大数据的新闻网站
#抓取新闻标题和链接
def extract_title(info):
pat = '<h3><a target=\"_blank\"(.*?)</a></h3>'
title = re.findall(pat, info)
titles='\n'.join(title)
#print titles
#修改指定字符串
titles1=titles.replace('class="title"','title')
titles2=titles1.replace('>',':')
titles3=titles2.replace('href','url:')
titles4=titles3.replace('="/','"http://www.itongji.cn/')
#写入文件
save=open('xinwen.txt','w')
save.write(titles4)
save.close()
titles = extract_title(doc)
希望本文所述对大家Python程序设计有所帮助。