Python抓取新闻标题和链接

来源:互联网 发布:win2008 80端口被占用 编辑:程序博客网 时间:2024/04/28 13:16
#-*-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) 
1 0
原创粉丝点击