python 3.5:爬虫--爬取人民日报1946-2003年所有新闻
来源:互联网 发布:jbqbld128enm编程 编辑:程序博客网 时间:2024/05/22 09:48
爬取资料库网站上的人民日报新闻(1946-2003)
总网址如下:
http://www.ziliaoku.org/rmrb?from=groupmessage&isappinstalled=0
从此网页开始爬取,进下一层,再进入下一层,再进行爬取。
由于本人还在学习过程中,有些很多其他更方便快捷的方法,以及一些爬虫功能还未用到,所以结果还是有两处需改进的地方,下面会上代码,欢迎一起讨论学习。
1.非按时间顺序出来文件(txt)
2.由于网站源代码的特殊,还未弄清如何爬取一天中每一版的,所以最后只能爬取每天的第一版,一天中每一版的网站都是同一个,版里每一条新闻都指向这个网站。
本次的爬取新闻是我学习爬虫的一个步骤过程,下次将发我运用scrapy爬取的一次实例
#coding=utf-8import requestsimport re # 正则表达式import bs4 # Beautiful Soup 4 解析模块import urllib.request # 网络访问模块import News #自己定义的新闻结构import codecs #解决编码问题的关键 ,使用codecs.open打开文件import sys #1解决不同页面编码问题import importlibimportlib.reload(sys)# 从首页获取所有链接def GetAllUrl(home): html = urllib.request.urlopen(home).read().decode('utf8') soup = bs4.BeautifulSoup(html, 'html.parser') pattern = 'http://www.ziliaoku.org/rmrb/[\d\S].*?' links = soup.find_all('a', href=re.compile(pattern)) for link in links: url_set.add(link['href'])def GetAllUrlL(home): html = urllib.request.urlopen(home).read().decode('utf8') soup = bs4.BeautifulSoup(html, 'html.parser') pattern = 'http://www.ziliaoku.org/rmrb/[\d\S].*?' links = soup.find_all('a', href=re.compile(pattern)) for link in links: url_set1.add(link['href'])def GetNews(url,i): response = requests.get(url) html = response.text article = News.News() try: article.title = re.findall(r'<h2 id=".*?">(.+?)</h2>', html) article.content = re.findall(r'<div class="article">([\w\W]*?)</div>', html) t = "" for j in article.title: t+=str('标题:'+j+'\n') c = "" for m in article.content: c+=str(m) article.content1 = ' ' + '\n'.join(c.split('')).strip() file = codecs.open('/tmp/luo/news '+str(i)+'.txt', 'w+') file.write(t+"\t"+article.content1) file.close() print('ok') except Exception as e: print('Error1:', e)def GetAllUrlK(home,i): html = urllib.request.urlopen(home).read().decode('utf8') soup = bs4.BeautifulSoup(html, 'html.parser') pattern = 'http://www.ziliaoku.org/rmrb/[\d\S].*?' link = soup.find('a', href=re.compile(pattern)) link1 = link['href'] print(link1) GetNews(link1,i)url_set = set() # url集合url_set1 = set() # url集合home = 'http://www.ziliaoku.org/rmrb?from=groupmessage&isappinstalled=0'GetAllUrl(home)try: for d in url_set: GetAllUrlL(d) print(d) i = 0 for b in url_set1: i = i+ 1 print(b) GetAllUrlK(b,i)except Exception as e: print('Error:', e)# home = 'http://www.ziliaoku.org/rmrb/1984-06-21'# i = 10# GetAllUrlK(home,i)
txt文件为新闻,格式可自己用正则去规范。
阅读全文
0 0
- python 3.5:爬虫--爬取人民日报1946-2003年所有新闻
- Python爬虫爬取网站新闻
- python爬虫爬取Bloomberg新闻
- python爬虫之爬取腾讯新闻
- python3爬虫-爬取新浪新闻首页所有新闻标题
- [python爬虫]使用Python爬取网易新闻
- (6)Python爬虫——爬取中新网新闻
- python爬虫爬取合肥工业大学校园新闻
- 第四课 Python爬虫简单爬取新浪新闻列表
- 使用python网络爬虫爬取新浪新闻(一)
- python爬取百度新闻所有的新闻的前1页 标题和URL地址
- python爬虫爬取csdn博客专家所有博客内容
- python 爬虫爬取所有上市公司公告信息(一)
- python 爬虫爬取所有上市公司公告信息(二)
- python 爬虫爬取所有上市公司公告信息(三)
- python 爬虫爬取所有上市公司公告信息(四)
- python 爬虫爬取所有上市公司公告信息(五)
- 爬虫实战一:爬取当当网所有 Python 书籍
- ssh登陆aix后,退格键无法删除
- [agc009b]Tournament
- 2017/10/17总结
- 拖拽外部文件进行读取-FileReader
- 数码相框设计-系统框架与环境
- python 3.5:爬虫--爬取人民日报1946-2003年所有新闻
- Maven学习笔记 (一)
- RDKit toolkit实战一:调用Python API
- 微软HololensMR的介绍(二)_完全使用指导教程
- UVALive 7139
- Redis
- 函数中关于const关键字使用的注意事项
- 无法使用真机或者Genymotion启动程序
- 内部类