在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  數(shù)據(jù)庫/ Scrapy 異步寫入mysql時部分?jǐn)?shù)據(jù)重復(fù)問題

Scrapy 異步寫入mysql時部分?jǐn)?shù)據(jù)重復(fù)問題

scrapy爬取百度新聞,參考網(wǎng)上回答修改Pipeline部分代碼:

def process_item(self,item,spider):
    asynItem = copy.deepcopy(item)
    query=self.dbpool.runInteraction(self.do_insert,asynItem)
    query.addErrback(self.handle_error)

def handle_error(self,failure):
    print(failure)

def do_insert(self,cursor,item):
    check_sql = "select count(*) from `company_news_scrapy` where (`link`,`company_name`) =({},{})".format("'" + item['link'] + "'","'" + item['company_name'] + "'")
    cursor.execute(check_sql)
    rest = cursor.fetchall()
    if rest[0]['count(*)']==0:
        insert_sql = "insert into `company_news_scrapy`(`company_name`,`link`) values(%s,%s);"
        cursor.execute(insert_sql, (item['company_name'],item['link']))
        

仍然出現(xiàn)部分?jǐn)?shù)據(jù)重復(fù)插入問題,求幫助。

回答
編輯回答
陪我終

會不會在你 select count(*) 之后,insert之前,別的線程插入了該數(shù)據(jù)呢?
要不你多打印點(diǎn)日志,看看問題出在哪。
另外,你可以試試:設(shè)置一下 唯一性約束,然后使用:
insert ... on duplicate key update ...
來操作:無則插入,有則更新

2018年1月19日 15:10
編輯回答
毀憶

同求。我知道原因,但是不知道怎么改,原因是因?yàn)閷憯?shù)據(jù)庫異步的,我們寫數(shù)據(jù)庫的時候直接拿數(shù)據(jù)的item的值,但是很有可能item的請求時間慢。這樣我們在下一次寫數(shù)據(jù)的時候就是寫的就還是上次的數(shù)據(jù)。

2018年7月28日 21:34
編輯回答
淚染裳

我也贊同樓上說的頁面新聞內(nèi)容重復(fù)或者就是自己點(diǎn)重復(fù)爬取

2018年3月21日 01:16
編輯回答
離觴

看不到你的爬取代碼
1.有可能是本身頁面數(shù)據(jù)重復(fù)問題,建議看一下網(wǎng)頁源代碼
2.有可能是爬取規(guī)則不夠明確導(dǎo)致的頁面重復(fù)爬取,建議看一下爬取規(guī)則是否完善

2018年5月14日 08:56