为啥存不到数据库呢?正常打印输出都没问题的... - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zdnyp
V2EX    Python

为啥存不到数据库呢?正常打印输出都没问题的...

  •  1
     
  •   zdnyp 2018-01-12 14:07:05 +08:00 2939 次点击
    这是一个创建于 2882 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #-*- conding:utf-8 -*-
    import requests,re,pymysql
    from pyquery import PyQuery as pq


    url_index = ['http://finance.eastmoney.com/news/cgnjj_{}.html'.format(pages) for pages in range(1,2)]

    def index_content(url_index,title=0,time=0):
    #print('起始页:' + url_index)
    index_req = requests.get(url_index,timeout = 10)
    index_req.encoding = 'utf-8'
    index_cOntent= index_req.text
    content_url = re.findall('<div class="text text-no-img">.*?<p class="title">.*?<a href="(.*?)" target="_blank">',index_content,re.S)
    title = re.search('<p class="title">.*?<a href=".*?" target="_blank">(.*?)</a>',index_content,re.S).group(1)
    time = re.search('<p class="time">(.*?)</p>',index_content,re.S).group(1).replace("年", "-").replace("月", "-").replace("日", "")
    #time = time_content
    #re.sub(".", "", title)
    #print('标题:' + title)
    #print('时间:' + time)
    return content_url

    def content(content_url):
    content_req = requests.get(content_url,timeout = 10)
    content_req.encoding = 'utf-8'
    cOntent= content_req.text
    title = re.search('<h1>(.*?)</h1>',content,re.S).group(1)
    time = re.search('<div class="time">(.*?)</div>',content,re.S).group(1).replace("年", "-").replace("月", "-").replace("日", "")
    source = re.search('<div class="source">.*?<span>来源:</span>(.*?)</div>',content,re.S).group(1)
    content_cOntent= re.search('<!--文章主体-->(.*?)<!--责任编辑-->',content,re.S).group(1)
    pic = re.findall('''.*?src="https://z1.dfcfw.com(.*?)"''',content_content,re.S)#.sub('.*?same.*?','',content_content)
    content_body = {'title:':title,
    'time:':time,
    'source:':source,
    'content:':content_content,
    'pic:':pic

    }
    # print('网址:' + content_url)
    # print ('标题:' + title)
    # print ('时间:' + time)
    # print ('来源:' + source)
    # print ('内容:' + content_content )
    # print (pic)
    #print(content_body)
    return content_body

    def save_sql(content_body):
    try:
    db = pymysql.connect("localhost","root","root","test" )
    cursor = db.cursor()
    sql = """INSERT INTO test(title,source_time, source, content_body, pageurl,pic)VALUES (title,time,source,content_content,content_url,pic)"""
    # 执行 sql 语句
    cursor.execute(sql)
    # 提交到数据库执行
    db.commit()
    except:
    # 如果发生错误则回滚
    db.rollback()
    # 关闭数据库连接
    db.close()

    for url_list in url_index:
    index_content(url_list,title=0,time=0)
    for con_url in content_url:
    con = content(con_url)
    #print(con)
    save_sql(con)
    8 条回复    2018-01-15 11:13:53 +08:00
        1
    sutra  
       2018-01-12 16:00:17 +08:00
    没有异常信息吗?
    frostming
        2
    frostming  
       2018-01-12 17:08:48 +08:00
    sql = """INSERT INTO test(title,source_time, source, content_body, pageurl,pic)VALUES (title,time,source,content_content,content_url,pic)"""
    你解析的信息呢?
    qingfengxm
        3
    qingfengxm  
       2018-01-12 18:02:57 +08:00
    source 是 sql 关键字吧
    GenHong
        4
    GenHong  
       2018-01-12 18:46:34 +08:00
    sql = """INSERT INTO test(title,source_time, source, content_body, pageurl,pic)VALUES (%s,%s,%s,%s,%s,%s)"""
    # 执行 sql 语句
    cursor.execute(sql, (title,time,source,content_content,content_url,pic))
    JerryCha
        5
    JerryCha  
       2018-01-12 20:43:56 +08:00
    except:
    # 如果发生错误则回滚
    db.rollback()

    except Exception as e:
    print(e)
    db.rollback()

    我猜楼主直接从 runoob 学的
    zdnyp
        6
    zdnyp  
    OP
       2018-01-15 09:23:31 +08:00
    @sutra 没有,只是存的都是 null 或者直接存字段名...
    zdnyp
        7
    zdnyp  
    OP
       2018-01-15 11:11:48 +08:00
    @sutra write() argument must be str, not dict 不能是字典...可是我没转成字典也不行啊
    zdnyp
        8
    zdnyp  
    OP
       2018-01-15 11:13:53 +08:00
    @GenHong 不能是字典类型的...
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1117 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 23:27 PVG 07:27 LAX 15:27 JFK 18:27
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86