Meta Blogging
Meta Blogging
由来
偶然想到说如果哪一天cnblogs挂了,那之前记录的随笔怎么办?可不可以把它们给download下来本地保存一份。正好看到有个库叫requests, 干嘛不试试看呢。
开工
有了requests 和 beautifulsoup,代码其实很简单。唯一需要注意的是,不能太频繁地用requests.get来抓取网页,不然会报错。一般的网站都会有类似的自我保护机制吧,防止被爬虫给爬死了。
import requests
from BeautifulSoup import BeautifulSoup
import re
import os
import time
URL='http://www.cnblogs.com/fangwenyu/p/'
URL_PATTERN = 'http://www.cnblogs.com/fangwenyu/p|archive'
pattern = re.compile(URL_PATTERN)
DIRECTORY = os.path.dirname(__file__)
ESCAPE_CHARS = '/\:*?"<>|' # Those characters are not allowed to be used in file name in Windows.
tbl = {ord(char): u'' for char in ESCAPE_CHARS}
# get the total page number
page_count = 0
resp = requests.get(URL)
if resp.status_code == requests.codes.ok:
soup = BeautifulSoup(resp.content)
attr = {'class':'Pager'}
result = soup.find('div', attr)
page_count = int(result.getText()[1:2])
with open(os.path.join(DIRECTORY, 'blog_archive.txt'), 'w') as blog_archive:
for page in range(1,page_count+1):
param = {'page':page}
resp = requests.get(URL, params=param)
soup = BeautifulSoup(resp.content, convertEntities=BeautifulSoup.HTML_ENTITIES)
blog_list = [(a.getText(), a.get('href')) for a in soup.findAll('a', id=True, href=pattern)]
for title, link in blog_list:
norm_title = title.translate(tbl)
item = '%s |[%s]| %s ' % (title, norm_title, link)
blog_archive.write(item.encode('utf-8'))
blog_archive.write('\n')
with open(os.path.join(DIRECTORY, norm_title + '.html'), 'w') as f:
f.write(requests.get(link).content)
# sleep for some time as access the cnblogs too freqently will cause the server not respond.
# Something like this --
# ...
# requests.exceptions.ConnectionError: ('Connection aborted.', error(10060, 'A connection attempt failed
# because the connected party did not properly respond after a period of time, or established connection failed
# because connected host has failed to respond'))
time.sleep(5)

Meta Blogging的更多相关文章
- 常用 meta 整理
<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 --> <meta name="HandheldFriendly" con ...
- meta标签
参考:http://www.jb51.net/web/158860.html META标签分两大部分:HTTP标题信息(HTTP-EQUIV)和页面描述信息(NAME). 一.HTTP标题信息(HTT ...
- Django模型类Meta元数据详解
转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...
- H5 meta小结
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1, ...
- Asp.net 后台添加CSS、JS、Meta标签
Asp.net 后台添加CSS.JS.Meta标签的写法,我这里写成函数方便以后使用.如果函数放在页面类中, Page参数也可以不要. 首先导入命名空间 using System.Web.UI.Htm ...
- 较为完整的meta
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 浏览器内核控制Meta标签说明文档【转】
背景介绍 由于众所周知的情况,国内的主流浏览器都是双核浏览器:基于Webkit内核用于常用网站的高速浏览.基于IE的内核用于兼容网银.旧版网站.以360的几款浏览器为例,我们优先通过Webkit内核渲 ...
- HTML <meta> 标签,搜索引擎
关于Mate标签的详尽解释,请查看w3school 网址为:http://www.w3school.com.cn/tags/tag_meta.asp meta标签作用 META标签是HTML标记HEA ...
- 内核控制Meta标签:让360浏览器默认使用极速模式打开网页(转)
为了让网站页面不那么臃肿,也懒的理IE了,同时兼顾更多的国内双核浏览器,在网页页头中添加了下面两行Meta控制标签. 1,网页头部加入 <meta name="renderer&quo ...
随机推荐
- linux下mysql-5.5.15安装详细步骤
linux下mysql-5.5.15安装详细步骤 注:该文档中用到的目录路径以及一些实际的值都是作为例子来用,具体的目录路径以各自安装时的环境为准 mysql运行时需要一个启动目录.一个安装目录和一个 ...
- 字符串的公共前缀对Mysql B+树查询影响回溯分析
年前项目组接微信公众号. 上线之后,跟微信相关的用cid列的查询会话的SQL变慢了几十倍!思考这个问题思考了非常久.从出现以来一直是我心头的一个结.cid这一列是建了索引的,普通的cid列更新 ...
- The connection to adb is down, and a severe error has occured. 错误
错误:The connection to adb is down, and a severe error has occured. [2010-03-11 09:36:56 - HelloOPone] ...
- Java 类型, Hibernate 映射类型及 SQL 类型之间的相应关系
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- MySQL user表简介
mysql> DESC MYSQL.USER \G *************************** . row *************************** Field: ...
- 解决 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”
eclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven.p ...
- MariaDB与MySQL并存
以下是MariaDB官方文档说明,MariaDB如何安装在已经存在MySQL实例的主机上: https://mariadb.com/kb/en/library/installing-mariadb-a ...
- adb shell settings 控制安卓系统设置(转)
Android4.2的源码android-17\com\android\commands目录下较之前的版本多了一个settings命令,查看其中的SettingsCmd.java文件,末尾有命令的帮助 ...
- php开发中sql语句拼接示例(插入、查询、更新)
1.插入语句 $sql="insert into Ad(AdClassID,AdType,AdTit,AdFileName,AdUrl,AShow,Addtime) values('&quo ...
- 原创:微信小程序调用PHP后台接口,解析纯html文本
---效果图片预览--- 1.微信js动态传参:wx.request({ url: 'https://m.****.com/index.php/Home/Xiaoxxf/activ ...