多線程版爬取故事網
阿新 • • 發佈:2017-09-03
實現 exe don comm value obj nco result nic
- 前言:
為了能以更高效的速度爬取,嘗試采用了多線程
本博客參照代碼及PROJECT來源:http://kexue.fm/archives/4385/
- 源代碼:
1 #! -*- coding:utf-8 -*- 2 import requests as rq 3 import re 4 import time 5 import datetime 6 import pymysql 7 from multiprocessing.dummy import Pool,Queue #dummy子庫是多線程庫 8 import html 9 from urllib.request import
- BUG:
- 數據庫並發寫入:
解答來源:https://stackoverflow.com/questions/6650940/interfaceerror-0
通過將遊標的創建移入線程,並在線程內關閉,跑出來的結果比之前好一些,但奇怪的是多幾行還是會出現並發報錯,奇怪,還能錯一半的?猜測將連接也放入線程會好些,或者幹脆不用commit提交?結果是不用commit都沒有寫入數據庫...我以為開啟了自動提交呢...
已解決,將數據庫連接和遊標都放在線程內創建 - 神奇地跳過一些數據庫裏面沒有的鏈接:
原來是過濾問題...水平真是...
已解決,修改URL過濾方式 - 編碼問題真是頭都大了...
encoding error : input conversion failed due to input error, bytes 0xB1 0x80 0xB5 0xC4
為什麽改了那麽多次還有...顯然是gbk轉utf8問題,可是我判斷了啊,還是有些網頁就是比較亂...
- 又是編碼問題:
UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\u30fb‘ in position 86: illegal multibyte sequence
- 網絡錯誤:
urllib.error.HTTPError: HTTP Error 503: Forwarding failure
多線程版爬取故事網