比这篇新的文章: Python生成Gravatar url的函数
比这篇旧的文章: GohanConsoleHelper

抓取糗事百科前100页的Python脚本

语言: Python, 标签: BeautifulSoup 美丽的汤 糗事百科 2008/05/23发布 1年前更新
作者: 半瓶墨水, 点击3066次, 评论(10), 收藏者(3), , 打分:

背景
主题: 字体:
01 #coding=utf-8
02 #需要BeautifulSoup(美丽的汤)支持:http://crummy.com/software/BeautifulSoup
03
04 import urllib
05 import urllib2
06 from xml.sax.saxutils import unescape
07 from BeautifulSoup import BeautifulSoup          # For processing HTML
08
09 def formalize(text):
10     result = ''
11     lines = text.split(u'\n')
12     for line in lines:
13         line = line.strip()
14         if len(line) == 0:
15             continue
16         result += line + u'\n\n'
17     return result
18
19 outfile = open("qiushi.txt", "w")
20 count = 0
21 for i in range(1, 101):
22     url = "http://qiushibaike.com/qiushi/best/all/page/%d" % i
23     data = urllib2.urlopen(url).readlines()
24     soup = BeautifulSoup("".join(data))
25     contents = soup.findAll('div', "content")
26     stories = [str(text) for text in contents]
27     for story in stories:
28         count += 1
29         print "processing page %d, %d items added" % (i, count)
30         minisoup = BeautifulSoup(story)
31         text = ''.join([e for e in minisoup.recursiveChildGenerator() if isinstance(e, unicode)])
32         text = urllib.unquote(unescape(text, {'"':'"'}))
33         text = formalize(text).encode("utf-8")
34         print >> outfile, '-' * 20 + " %05d " % count + '-' * 20 + "\n"
35         print >> outfile, text + "\r\n"
36 outfile.close()


所有评论,共10条:( 我也来说两句)

1
Bruce 1年前 回复
0
1
,不错,不错:)
2
ok 1年前 回复
2
1
@Bruce
欢迎光临
唉,没有预期的需求多啊,现在还没有100个用户呢
3
测试 1年前 回复
1
1
@2: 测试回帖内联链接
4
Bruce 1年前 回复
2
2
加油加油,我觉得挺好用的。
5
jfxwc 1年前 回复
0
2
非常不错的代码,很有参考价值。
6
hakalv 1年前 回复
0
0
很多标准库不知道有什么用
7
liuxin9023 1年前 回复
0
0
#糗事百科完美下载版
代码太长不给发
http://liuxin9023.blog.163.com/blog/static/7199890220081069829917/
8
0
0
@7 哈哈,前两天还到你blog上回复了一下,原来就是你啊
看起来不是非常长,应该能发的啊
9
solu 11个月前 回复
0
0
收藏啦
10
teloon 6个月前 回复
0
0
cool~ 不过我先学Python去。。。

发表评论

注册登录后再发表评论