【发布时间】:2017-09-06 05:15:11
【问题描述】:
我的蜘蛛可以工作,但我无法下载我在 .html 文件中抓取的网站的正文。如果我写 self.html_fil.write('test') 那么它工作正常。我不知道如何将 tulpe 转换为字符串。
我使用 Python 3.6
蜘蛛:
class ExampleSpider(scrapy.Spider):
name = "example"
allowed_domains = ['google.com']
start_urls = ['http://google.com/']
def __init__(self):
self.path_to_html = html_path + 'index.html'
self.path_to_header = header_path + 'index.html'
self.html_file = open(self.path_to_html, 'w')
def parse(self, response):
url = response.url
self.html_file.write(response.body)
self.html_file.close()
yield {
'url': url
}
追踪:
Traceback (most recent call last):
File "c:\python\python36-32\lib\site-packages\twisted\internet\defer.py", line
653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "c:\Users\kv\AtomProjects\example_project\example_bot\example_bot\spiders
\example.py", line 35, in parse
self.html_file.write(response.body)
TypeError: write() argument must be str, not bytes
【问题讨论】:
-
试试 response.body.decode("utf-8")
标签: python django scrapy web-crawler