这两天上班接手,别人留下来的爬虫发现一个很好玩的 SQL脚本拼接。只要你的Scrapy Field字段名字和 数据库字段的名字 一样。那么恭喜你你就可以拷贝这段SQL拼接脚本。进行MySQL入库处理。具体拼接代码如下:def process_item(self, item, spider):
if isinstance(item, WhoscoredNewItem):
table_name = item.pop('table_name')
col_str = ''
row_str = ''
for key in item.keys():
col_str = col_str + " " + key + ","
row_str = "{}'{}',".format(row_str, item[key] if "'" not in item[key] else item[key].replace("'", "\\'"))
sql = "insert INTO {} ({}) VALUES ({}) ON DUPLICATE KEY UPDATE ".format(table_name
...
继续阅读
(20)