Python爬虫QQnews
2014-02-26 13:29
239 查看
好久没碰python了。。写一个简单的热热手
''' Created on 2014.2.25 for QQnews @author: accyao ''' import sys import urllib2 import urllib import re import os import time reload(sys) sys.setdefaultencoding('utf-8') headers = { 'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' } tm = time.strftime('%Y%m%d',time.localtime(time.time())) print(tm) def getTitle(page): key = "<title>.*</title>" title = re.findall(key, page) return title def getText(page): key = "<P style=\"TEXT-INDENT: 2em\">.*</P>" text = re.findall(key,page) return text def dlNews(idx): tmp = urllib2.Request( url = 'http://news.qq.com/a/'+str(tm)+'/'+str("%06d"%idx)+'.htm', headers = headers ) try: html = urllib2.urlopen(tmp).read() title = getTitle(html)[0] title = title.replace('<title>','') title = title.replace('</title>','') if(os.path.isfile(title+".txt")==0): filename = title+".txt" fl = file(filename,'w') text = getText(html)[0] text = text.replace('<P style=\"TEXT-INDENT: 2em\">','') text = text.replace('</P>','\n') text = re.sub('<[^>]*>',' ',text) fl.write(text) except urllib2.HTTPError,e: print(e.reason) def main(): for i in range(1803,1804): dlNews(i) main()
相关文章推荐
- python3 [爬虫入门实战] 爬虫之selenium 模拟QQ登陆抓取好友说说内容(暂留)
- Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs)
- Python开源爬虫项目代码:抓取淘宝、京东、QQ、知网数据--转
- Python+Selenium爬虫实战一《将QQ今日话题发布到个人博客》
- 简单的Python抓taobao图片爬虫
- 爬虫开发python工具包介绍 (2)
- 【Python爬虫学习笔记(3)】Beautiful Soup库相关知识点总结
- Python爬虫利器之PhantomJS的用法
- Python 爬虫(1)基础知识和简单爬虫
- 最近百度云盘不提供搜索,闲来无事,玩玩python爬虫,爬一下百度云盘的资源
- 用Python制作简单的爬虫---爬虫基本思想
- Python2 爬虫(九) -- Scrapy & BeautifulSoup之再爬CSDN博文
- python爬虫得到《三国演义》
- Python实现爬虫设置代理IP和伪装成浏览器的方法分享
- Python爬虫实战(1):爬取糗事百科段子
- Python爬虫实例一
- python爬虫项目- 爬取厦门航空官网机票价格 selenium 多进程爬取
- 【Python3 爬虫】08_正则表达式(元字符与语法)
- 第三课 Python爬虫Beautifulsoup4模块的使用