forked from yaojialyu/crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl_title.py
86 lines (70 loc) · 2.44 KB
/
crawl_title.py
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
#coding:utf8
"""
此脚本读入所有的topic列表,抓取title值,并重新存入
"""
from urlparse import urljoin,urlparse
import logging
from lxml import etree # use XPath from lxml
import codecs
import pdb
import time
from bs4 import BeautifulSoup
from threadPool import ThreadPool
from logconfig import congifLogger
from webPage import WebPage
import stacktracer
# config logging
log = logging.getLogger('Main.crawl_title')
congifLogger("crawl_title.log", 5)
failed_set = set()
def task_handler(topic_id, seg_list):
f = codecs.open('tables/TopicInfo-title.txt', 'a','utf-8')
url = 'http://www.douban.com/group/topic/' + topic_id + '/'
print 'Visiting: ', url
webPage = WebPage(url)
flag = webPage.fetch()
if flag:
url, pageSource = webPage.getDatas() # pageSource已经为unicode格式
page = etree.HTML(pageSource)
content = page.xpath(u"/html/body/div[@id='wrapper']/div[@id='content']")[0]
tmp = page.xpath(u"//table[@class='infobox']//td[@class='tablecc']")
if len(tmp) == 0:
# 标题没有被截断
titlenode = content.xpath("h1")[0]
title = titlenode.text.strip()
else:
titlenode = tmp[0]
title = etree.tostring(titlenode, method='text', encoding='utf-8').strip()
if isinstance(title, unicode):
pass
else:
title = title.decode("utf-8")
seg_list.insert(4, title)
f.write('[=]'.join(seg_list) + '\n')
else:
failed_set.add(topic_id)
f.close()
def main():
threadPool = ThreadPool(5)
threadPool.startThreads()
f = codecs.open('tables/TopicInfo-all.txt', 'r', 'utf-8') # 读入unicode字符
count = 0
for line in f:
line = line.strip()
seg_list = line.split('[=]')
if seg_list[1] == 'ustv':
threadPool.putTask(task_handler, seg_list[0], seg_list)
count += 1
f.close()
while threadPool.getTaskLeft() > 0:
time.sleep(10)
print 'Waiting to finish. Task left: %d' % threadPool.getTaskLeft()
log.info('Number of topics in ustv: %d' % count)
if __name__ == '__main__':
stacktracer.trace_start("trace.html",interval=5,auto=True) # Set auto flag to always update file!
main()
stacktracer.trace_stop()
log.info('抓取失败的topic id:')
for tid in failed_set:
log.info(tid)
log.info('抽取title结束')