python 3.5:爬虫--爬取人民日报1946-2003年所有新闻
2017-10-17 22:13
489 查看
爬取资料库网站上的人民日报新闻(1946-2003)
总网址如下:
http://www.ziliaoku.org/rmrb?from=groupmessage&isappinstalled=0
从此网页开始爬取,进下一层,再进入下一层,再进行爬取。
由于本人还在学习过程中,有些很多其他更方便快捷的方法,以及一些爬虫功能还未用到,所以结果还是有两处需改进的地方,下面会上代码,欢迎一起讨论学习。
1.非按时间顺序出来文件(txt)
2.由于网站源代码的特殊,还未弄清如何爬取一天中每一版的,所以最后只能爬取每天的第一版,一天中每一版的网站都是同一个,版里每一条新闻都指向这个网站。
本次的爬取新闻是我学习爬虫的一个步骤过程,下次将发我运用scrapy爬取的一次实例
#coding=utf-8
import requests
import re # 正则表达式
import bs4 # Beautiful Soup 4 解析模块
import urllib.request # 网络访问模块
import News #自己定义的新闻结构
import codecs #解决编码问题的关键 ,使用codecs.open打开文件
import sys #1解决不同页面编码问题
import importlib
importlib.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(
4000
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文件为新闻,格式可自己用正则去规范。
总网址如下:
http://www.ziliaoku.org/rmrb?from=groupmessage&isappinstalled=0
从此网页开始爬取,进下一层,再进入下一层,再进行爬取。
由于本人还在学习过程中,有些很多其他更方便快捷的方法,以及一些爬虫功能还未用到,所以结果还是有两处需改进的地方,下面会上代码,欢迎一起讨论学习。
1.非按时间顺序出来文件(txt)
2.由于网站源代码的特殊,还未弄清如何爬取一天中每一版的,所以最后只能爬取每天的第一版,一天中每一版的网站都是同一个,版里每一条新闻都指向这个网站。
本次的爬取新闻是我学习爬虫的一个步骤过程,下次将发我运用scrapy爬取的一次实例
#coding=utf-8
import requests
import re # 正则表达式
import bs4 # Beautiful Soup 4 解析模块
import urllib.request # 网络访问模块
import News #自己定义的新闻结构
import codecs #解决编码问题的关键 ,使用codecs.open打开文件
import sys #1解决不同页面编码问题
import importlib
importlib.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(
4000
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文件为新闻,格式可自己用正则去规范。
相关文章推荐
- python3.5 beautiful4.4 扣扣国内新闻 爬虫
- Python爬虫项目,获取所有网站上的新闻,并保存到数据库中,解析html网页等(未完待续)
- python3爬虫-爬取新浪新闻首页所有新闻标题
- python爬虫,爬取虎扑网新闻
- Python爬虫框架Scrapy 学习笔记 10.1 -------【实战】 抓取天猫某网店所有宝贝详情
- 【python】爬虫2——下载亦舒博客首页所有文章
- Python爬虫:抓取新浪新闻数据
- python简单应用!用爬虫来采集天猫所有优惠券信息,写入本地文件
- python爬虫之 selenium + phantomjs 爬斗鱼所有直播间
- python爬虫爬取合肥工业大学校园新闻
- Python爬虫-爬取 ygdy8.com 站点的所有电影并写入文件和数据库
- python 爬虫实战(三)使用pyspider爬取虎嗅新闻
- python爬虫——获取新闻标题
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- Python爬虫小实践:获取某个网站所有的外部链接以及内部链接
- Python 爬虫笔记(获取整个站点中的所有外部链接)
- 基于Scrapy框架的Python新闻爬虫
- python3爬虫 爬取图片,爬取新闻网站文章并保存到数据库
- python scrapy多进程新闻爬虫
- 【学习笔记】Python爬虫-豆瓣电影所有短评