#coding:utf8
from baike_spider import url_manager, html_downloader, html_parser,\
html_outputer
class SpiderMain(object):
#构造函数,初始化各个对象
def __init__(self):
#url管理器
self.urls = url_manager.UrlManager()
#url下载器
self.downloader = html_downloader.HtmlDownloader()
#url解析器
self.parser = html_parser.HtmlParser()
#url输出器
self.outputer =html_outputer.HtmlOutputer()
#爬虫的调度程序
def craw(self, root_url):
#count记录当前爬取的是第几个url
count = 1
#1.首先将入口url添加入管理器
self.urls.add_new_url(root_url)
#启动爬虫循环,当管理器中有url时
while self.urls.has_new_url():
try:
#获取一个待爬取的url
new_url = self.urls.get_new_url()
print ('craw %d : %s' % (count,new_url))
#启动下载器下载页面
html_cont = self.downloader.download(new_url)
#调用解析器解析页面数据,返回新的url列表和新的数据
new_urls,new_data = self.parser.parse(new_url,html_cont)
#将新的url补充到管理器
self.urls.add_new_urls(new_urls)
#收集数据
self.outputer.collect_data(new_data)
#爬取1000个目标
if count == 1000:
break
count = count + 1
except:
print 'craw failed'
#输出收集好的数据
self.outputer.output_html()
#1.首先编写main函数
if __name__=="_main_":
#2.编写入口的url
root_url = "http://baike.baidu.com/view/21087.htm"
obj_spider = SpiderMain()
#3.启动爬虫
obj_spider.craw(root_url)
7 个解决方案
#1
报什么错呢,写清楚吧
#2
baike_spider是不是没有这个库
#3
pydev debugger: starting (pid: 4200)
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-6: ordinal not in range(128)
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-6: ordinal not in range(128)
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
#4
麻烦你贴的时候注意下格式,看得头好晕
#5
编码问题,ASCII标准来解码只能解码特殊的128个字符,而遇到128字符以外的就会出现编码错误
#6
编码解码问题,老问题了。
#7
参考一下:http://brucedone.com/archives/43
#1
报什么错呢,写清楚吧
#2
baike_spider是不是没有这个库
#3
pydev debugger: starting (pid: 4200)
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-6: ordinal not in range(128)
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-6: ordinal not in range(128)
Traceback (most recent call last):
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_comm.py", line 1080, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "F:\eclipse\eclipse\plugins\org.python.pydev_5.1.2.201606231256\pysrc\_pydevd_bundle\pydevd_vars.py", line 344, in evaluate_expression
expression = str(expression.replace('@LINE@', '\n'))
#4
麻烦你贴的时候注意下格式,看得头好晕
#5
编码问题,ASCII标准来解码只能解码特殊的128个字符,而遇到128字符以外的就会出现编码错误
#6
编码解码问题,老问题了。
#7
参考一下:http://brucedone.com/archives/43