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 · 2500 次点击
    这是一个创建于 2514 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #-*- 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
    sutra
        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   ·   实用小工具   ·   2543 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 05:07 · PVG 13:07 · LAX 21:07 · JFK 00:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.