- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python实现批量下载新浪博客的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了python实现批量下载新浪博客的方法。分享给大家供大家参考。具体实现方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# coding=utf-8
import
urllib2
import
sys, os
import
re
import
string
from
BeautifulSoup
import
BeautifulSoup
def
encode(s):
return
s.decode(
'utf-8'
).encode(sys.stdout.encoding,
'ignore'
)
def
getHTML(url):
#proxy_handler = urllib2.ProxyHandler({'http':'http://211.138.124.211:80'})
#opener = urllib2.build_opener(proxy_handler)
#urllib2.install_opener(opener)
req
=
urllib2.Request(url)
response
=
urllib2.urlopen(req, timeout
=
15
)
return
BeautifulSoup(response, convertEntities
=
BeautifulSoup.HTML_ENTITIES)
def
visible(element):
'''抓取可见的文本元素'''
if
element.parent.name
in
[
'style'
,
'script'
,
'[document]'
,
'head'
,
'title'
]:
return
False
elif
re.match(
'<!--.*-->'
,
str
(element)):
return
False
elif
element
=
=
u
'\xa0'
:
return
False
return
True
def
delReturn(element):
'''删除元素内的换行'''
return
re.sub(
'(?<!^)\n+(?!$)'
,
' '
,
str
(element)).decode(
'utf-8'
)
def
validFilename(filename):
# windows
return
re.sub(
'[\/:*?<>"|\xa0]'
, '', filename)
def
writeToFile(text, filename, dirname):
if
not
os.path.exists(dirname):
os.makedirs(dirname)
print
encode(
'保存到目录'
), dirname
filename
=
validFilename(filename)
print
encode(
'保存文章'
), filename
path
=
os.path.join(dirname, filename)
if
not
os.path.exists(path):
f
=
open
(path,
'w'
)
f.write(text)
f.close()
else
:
print
filename, encode(
'已经存在'
)
def
formatContent(url, title
=
''):
'''格式化文章内容'''
page
=
getHTML(url)
content
=
page.find(
'div'
, {
'class'
:
'articalContent'
})
art_id
=
re.search(
'blog_(\w+)\.html'
, url).group(
1
)
blog_name
=
page.find(
'span'
,
id
=
'blognamespan'
).string
if
title
=
=
'':
title
=
page.find(
'h2'
,
id
=
re.
compile
(
'^t_'
)).string
temp_data
=
filter
(visible, content.findAll(text
=
True
))
# 去掉不可见元素
temp_data
=
''.join(
map
(delReturn, temp_data))
# 删除元素内的换行符
temp_data
=
temp_data.strip()
# 删除文章首尾的空行
temp_data
=
re.sub(
'\n{2,}'
,
'\n\n'
, temp_data)
# 删除文章内过多的空行
# 输出到文件
# 编码问题
temp_data
=
'本文地址:'
.decode(
'utf-8'
)
+
url
+
'\n\n'
+
temp_data
op_text
=
temp_data.encode(
'utf-8'
)
op_file
=
title
+
'_'
+
art_id
+
'.txt'
writeToFile(op_text, op_file, blog_name)
def
articlelist(url):
articles
=
{}
page
=
getHTML(url)
pages
=
page.find(
'ul'
, {
'class'
:
'SG_pages'
}).span.string
page_num
=
int
(re.search(
'(\d+)'
, pages).group(
1
))
for
i
in
range
(
1
, page_num
+
1
):
print
encode(
'生成第%d页文章索引'
%
i)
if
i !
=
1
:
url
=
re.sub(
'(_)\d+(\.html)$'
,
'\g<1>'
+
str
(i)
+
'\g<2>'
, url)
page
=
getHTML(url)
article
=
page.findAll(
'span'
, {
'class'
:
'atc_title'
})
for
art
in
article:
art_title
=
art.a[
'title'
]
art_href
=
art.a[
'href'
]
articles[art_title]
=
art_href
return
articles
def
blog_dld(articles):
if
not
isinstance
(articles,
dict
):
return
False
print
encode(
'开始下载文章'
)
for
art_title, art_href
in
articles.items():
formatContent(art_href, art_title)
if
__name__
=
=
'__main__'
:
sel
=
raw_input
(encode(
'你要下载的是(1)全部文章还是(2)单篇文章,输入1或者2: '
))
if
sel
=
=
'1'
:
#articlelist_url = 'http://blog.sina.com.cn/s/articlelist_1303481411_0_1.html'
articlelist_url
=
raw_input
(encode(
'请输入博客文章目录链接: '
))
articles
=
articlelist(articlelist_url)
blog_dld(articles)
else
:
#article_url = 'http://blog.sina.com.cn/s/blog_4db18c430100gxc5.html'
article_url
=
raw_input
(encode(
'请输入博客文章链接: '
))
formatContent(article_url)
|
希望本文所述对大家的Python程序设计有所帮助.
最后此篇关于python实现批量下载新浪博客的方法的文章就讲到这里了,如果你想了解更多关于python实现批量下载新浪博客的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
当我尝试通过我的 .exe 文件从 url 下载 .pdf 文件时出现以下错误。 The server committed a protocol violation. Section=Response
我是一家非营利组织的 G Suite 管理员,刚刚发现数据导出功能,这似乎是个人帐户的外卖。 导出文件已准备好,现在可以从 Google Cloud Platform Storage 中的存储桶下载。
导航 引言 总体思路 七牛云相关的配置文件 获取七牛云上传token 相关类定义 核心代码实现 获取七牛云图片下载链接 公开空
这不是后端编程问题。我只能修改标记或脚本(或文档本身)。我在这里问的原因是因为我对适当术语的所有搜索都不可避免地导致有关编程此功能的问题和解决方案。我不是试图通过编程来强制它;我必须找出此 PDF 行
您好,我已在 Google AdSense 中注册,我想使用适用于 iOS 的 SDK,但目前我找不到 SDK 下载链接。 我的申请已获批准。 任何人都知道如何下载这个sdk。 我使用这个链接来描述如
我需要为当前在 SourceForge 上的 github 项目提供二进制文件和文档。在那里,我可以为我需要的下载提供一个目录结构,因为我必须为大约 10 个不同的操作系统提供几个版本。 github
我从 Canvas 下载绘图时遇到问题。这是我的代码: function downloadCanvas(link, canvasId, filename) { link.href =
ASP.NET 项目 我将使用 Azure 进行存储。问题(要求): 在我的项目中,我让注册用户下载文件。但我不希望用户将此下载链接分享给未注册的人(例如:我给注册用户的下载链接只能在他们的计算机上下
我编写了一个servlet,用于检查http header ,但我不知道为什么当页面加载时,它会自动开始下载。 /* * To change this template, choose To
我正在尝试将下载添加到我的网络浏览器,但遇到的问题是获取您尝试下载的文件的名称。这是我的下载代码: engine.locationProperty().addListener(new ChangeLi
我正在尝试下载网站的 html: String encoding = "UTF-8"; HttpContext localContext = new BasicHttpContext();
我制作了一个带有“开始下载”按钮的框架,用于从网站下载 JAR。 问题是每当我点击开始下载按钮时,整个框架就会卡住,直到下载完成,然后就正常了。 我该如何解决这个问题? 这是单击按钮时执行的代码 p
我得到这段代码来实现一些东西,它可以帮助我从给定的 URL 下载文件。 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSes
我正在尝试创建一个 Controller 来使用流方法下载和上传文件,在我的例子中,所有文件都作为 Blob 保存在数据库中。我阅读了 Jboss Netty 的文档,但我认为这不是我的最佳解决方案。
下载并保存文件 let destination: DownloadRequest.DownloadFileDestination = { _, _ in // var fileURL = sel
使用 htaccess 我基本上试图禁止访问该页面,即 http://example.com , 但它仍然允许人们下载文件,如果他们有直接链接即 http://example.com/hi.zip .
我正在寻求将脚本与我的控制面板集成,并且由于我是新手脚本编写者而遇到问题。我想做的是用 1 个脚本下载一个文件并解压它。 示例: wget http://example.com/example.tar
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
这个问题在这里已经有了答案: Top techniques to avoid 'data scraping' from a website database (14 个答案) 关闭 5 年前。 我有
这个问题在这里已经有了答案: Reading and parsing email from Gmail using C#, C++ or Python (6 个答案) 关闭 7 年前。 我只是想,是
我是一名优秀的程序员,十分优秀!