python实现kindle每天推送博客2----python实现爬取博客内容
来源:互联网 发布:java代码混淆加密工具 编辑:程序博客网 时间:2024/04/29 17:47
python爬虫教程很多,
本文以爬取博客为例
1.
Beautiful Soup
Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据
为节约篇幅,安装方法自行百度
解析器:
下表列出了主要的解析器,以及它们的优缺点:
- Python的内置标准库
- 执行速度适中
- 文档容错能力强
- Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
- 速度快
- 文档容错能力强
- 需要安装C语言库
BeautifulSoup(markup, ["lxml","xml"])
BeautifulSoup(markup, "xml")
- 速度快
- 唯一支持XML的解析器
- 需要安装C语言库
- 最好的容错性
- 以浏览器的方式解析文档
- 生成HTML5格式的文档
- 速度慢
- 不依赖外部扩展
推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前的版本,必须安装lxml或html5lib, 因为那些Python版本的标准库中内置的HTML解析方法不够稳定.
用法简单介绍:
from bs4 import BeautifulSoupsoup = BeautifulSoup(open("index.html"))print(soup.prettify())soup.title# <title>The Dormouse's story</title>soup.title.name# u'title'soup.title.string# u'The Dormouse's story'soup.title.parent.name# u'head'soup.p# <p class="title"><b>The Dormouse's story</b></p>soup.p['class']# u'title'soup.a# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>soup.find_all('a')# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]soup.find(id="link3")# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>从文档中找到所有<a>标签的链接:for link in soup.find_all('a'): print(link.get('href')) # http://example.com/elsie # http://example.com/lacie # http://example.com/tillie从文档中获取所有文字内容:print(soup.get_text())# The Dormouse's story#
说明:这个get_text( )特别好用,因为我要把文章内容提取出来,之前考虑用正则去除哪些网页标签符合,也有用nltk来去除的,后来发现直接可以用这个方法。
2.介绍这么多,下面上我的工程源码
#!/usr/bin/env python#coding=utf-8## Copyright 2017 liuxinxing#from bs4 import BeautifulSoupimport urllib2import datetimeimport timeimport PyRSS2Genimport reimport sysreload(sys)sys.setdefaultencoding('utf-8')class RssSpider(): def __init__(self): self.myrss = PyRSS2Gen.RSS2(title='OSChina', link='http://my.oschina.net', description=str(datetime.date.today()), pubDate=datetime.datetime.now(), lastBuildDate = datetime.datetime.now(), items=[] ) self.xmlpath=r'./oschina.xml' self.baseurl="http://www.oschina.net/blog" #if os.path.isfile(self.xmlpath): #os.remove(self.xmlpath) def useragent(self,url): i_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36","Referer": 'http://baidu.com/'} req = urllib2.Request(url, headers=i_headers) html = urllib2.urlopen(req).read() return html def enterpage(self,url): pattern = re.compile(r'\d{4}\S\d{2}\S\d{2}\s\d{2}\S\d{2}') rsp=self.useragent(url) # print rsp soup=BeautifulSoup(rsp, "html.parser") # print soup timespan=soup.find('div',{'class':'blog-content'}) # print timespan timespan=str(timespan).strip().replace('\n','').decode('utf-8') # match=re.search(r'\d{4}\S\d{2}\S\d{2}\s\d{2}\S\d{2}',timespan) # timestr=str(datetime.date.today()) # if match: # timestr=match.group() #print timestr ititle=soup.title.string print ititle div=soup.find('div',{'class':'BlogContent'}) # print type(div) doc = div.get_text() # print type(doc) return ititle,doc def getcontent(self): rsp=self.useragent(self.baseurl) # print rsp soup=BeautifulSoup(rsp, "html.parser") # print soup ul=soup.find('div',{'id':'topsOfRecommend'}) # print ul for div in ul.findAll('div',{'class':'box-aw'}): # div=li.find('div') # print div if div is not None: alink=div.find('a') if alink is not None: link=alink.get('href') print link if self.isbloglink(link): title,doc =self.enterpage(link) self.savefile(title,doc) def isbloglink(self,link): express = r".*/blog/.*" mo = re.search(express, link) if mo: return True else: return False def savefile(self,title,doc): doc = doc.decode('utf-8') with open("./data/"+title+".txt",'w') as f: f.write(doc)if __name__=='__main__': rssSpider=RssSpider() rssSpider.getcontent() # rssSpider.enterpage("https://my.oschina.net/diluga/blog/1501203")
文件中还有一些生成rss的东西,可以忽略,因为最开始想做成爬取后生成rss源,后来想直接推送给kindle就没用了。
参考:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
阅读全文
1 0
- python实现kindle每天推送博客2----python实现爬取博客内容
- python实现kindle每天推送博客1----kindle推送原理,python实现qq邮箱登录及邮件发送
- python爬取csdn的博客内容
- python爬虫爬取csdn博客专家所有博客内容
- Python爬取CSDN博客
- python+flask实现简单博客
- python urllib2实现刷博客
- python爬取oschina博客列表
- 使用python爬取csdn博客访问量
- 使用python爬取csdn博客访问量
- 使用python爬取csdn博客访问量
- 使用python爬取csdn博客访问量
- python爬取博客标题和访问量
- Python 爬取CSDN博客频道
- Python爬虫爬取博客访问量
- 使用python爬取csdn博客访问量
- Scrapy爬取博客内容
- python_爬取博客内容
- POWER10将于2020年前后推出 届时将采用10nm制程
- HDU2553-回溯-N皇后问题
- TextView 代码中设置字体加粗
- 2017年最新苹果开发者账号注册申请流程最强详解!
- linux如何实现signal?
- python实现kindle每天推送博客2----python实现爬取博客内容
- zookeeper的常用命令
- 关于算法竞赛入门经典前四张总结
- 使用jQuery中 select option:selected 实现2个下拉框之间数据的交换
- 《深入浅出java虚拟机》读书笔记
- iOS学习资料
- Spring中ApplicationContext和beanfactory区别
- df
- yii2加密方法