问题背景:指定爬虫depth、线程数, python实现并行爬虫
思路: 单线程 实现爬虫类Fetcher
多线程 threading.Thread去调Fetcher
方法:Fetcher 中,用urllib.urlopen打开指定url,读取信息:
response = urllib.urlopen(self.url) content = response.read()但是这样有问题, 比如对于www.sina.com来说,读出来的content是乱码的:
>>> content[0:100]
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xec\xbdk\x93\x1c\xd7u \xf8\x99\x8c\xd0\x7fH\x14W\xe8*t=2\xeb\xd5\xd5]H`\x014@4\x88\x97\x00\xf0%\x10\xea\xc8\xaa\xca\xeeN\xa0\xba\xb2X\x99\x85\x06X\xa8\x1fCj\x1c\xb6ly-\x92\x06\xf5 %\xca"E\xf1!R\x94\xa8\x87C3\x9e\xf1\xd8#\x87\xbd;\x8e\xd8\x99\x8d\xb1\x1d\xf2'
于是用了python第三方工具chardet,通过
chardet.detect(content)进行content中字符集的检测: