数据写入数据库错误,提示'invalid literal for int() with base 10: \\'1,094\\''

来源:6-21 保存数据到mysql中 -3

qq_祢奇_0

2018-12-03

错误提示:http://img.mukewang.com/szimg/5c0532e40001139312760291.jpg

代码内容: 

def get_insert_sql(self):
   # 插入知乎question表的sql语句
   insert_sql = """
       insert into zhihu_question(zhihu_id, topics, url, title, content, answer_num, comments_num,
         watch_user_num, click_num, crawl_time
         )
       VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
       ON DUPLICATE KEY UPDATE content=VALUES(content), answer_num=VALUES(answer_num),
       comments_num=VALUES(comments_num),
         watch_user_num=VALUES(watch_user_num), click_num=VALUES(click_num)
   """
   zhihu_id = self["zhihu_id"][0]
   topics = ",".join(self["topics"])
   url = self["url"][0]
   title = "".join(self["title"])
   content = "".join(self["content"])
   answer_num = extract_num("".join(self["answer_num"]))
   comments_num = extract_num("".join(self["comments_num"]))
   if len (self["watch_user_num"]) == 2:
       watch_user_num = int(self["watch_user_num"][0])
       click_num = int(self["watch_user_num"][1])
   else:
       watch_user_num = int(self["watch_user_num"][0])
       click_num = 0
   crawl_time = datetime.datetime.now().strftime(SQL_DATETIME_FORMAT)
   params = (zhihu_id, topics, url, title, content, answer_num, comments_num,
             watch_user_num, click_num, crawl_time)
   return insert_sql, params

写回答

1回答

bobby

2018-12-05

这个是因为你//img.mukewang.com/szimg/5c072ecd0001778107230204.jpg 你调用int强转的时候, 如果字符串中包含非数字的字符串 比如一个数字字符串中包含了逗号 比如 “1,094”在页面是其实是1094的意思,但是因为你没有处理掉逗号,造成了字符串转int会抛异常

0
0

Scrapy打造搜索引擎 畅销4年的Python分布式爬虫课

带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎

5796 学习 · 6290 问题

查看课程