`conn = pymysql.connect(host = '127.0.0.1' , port = 3306 , user = 'root' , password = 'root' , db = 'dedao')
cor = conn.cursor()`
`def column():
req = requests.post(url , headers = headers ,data = post_data).text
title = re.findall('''"id":.*?,"title":"(.*?)","''',req,re.S)
config_pic = re.findall('"config":"(.*?)","',req,re.S)
description = re.findall('"intro":"(.*?)",',req,re.S)
price = re.findall(',"price_str":"(.*?)",',req,re.S)
notice = re.findall('"notice":"(.*?)",',req,re.S)
subnum = re.findall(',"subnum":(.*?),"',req,re.S)
price_desc = re.findall(',"price_desc":"(.*?)",',req,re.S)
log_id = re.findall(',"log_id":"(.*?)",',req,re.S)
log_type =re.findall('"log_type":"(.*?)"',req,re.S)
name = re.findall('"name":"(.*?)"',req,re.S)
intro = re.findall('"intro":"(.*?)",',req,re.S)
for title in title:
sql = '''insert into columns(title) VALUE ('title')'''
print(title)
for config_pic in config_pic:
print(config_pic)
for description in description:
print(description)
for price in price:
print(price)
for notice in notice:
print(notice)
for subnum in subnum:
print(subnum)
for price_desc in price_desc:
print(price_desc)
for log_id in log_id:
print(log_id)
for log_type in log_type:
print(log_type)
for name in name:
print(name)
for intro in intro:
print(intro)
return title
column()`
--------------------------------------------------------------------------------------------------------
代码如上,我在一个for循环里使用了insert,但是运行之后,表里没有插入新的数据,而且也没报错,请问什么原因呢?
尝试了把插入操作挪到函数的调用环节,就提示title未定义,代码是这样的:
`for title in title:
column()
sql = '''insert into columns(title) VALUE ('title')'''`
只是文本,没有被执行,更没有错误
for title in title:
sql = '''insert into columns(title) VALUE (title)'''
conn.commit(sql)
print(title)
额,这段加了提交,说是参数少了commit() takes 1 positional argument but 2 were given
建议给代码加上高亮
– dudu 6年前@dudu: 不太会用Markdown...我重新编辑下
– 会发光 6年前