zoukankan      html  css  js  c++  java
  • 爬取数据并插入mysql数据库

    #!/usr/bin/env python
    # coding=utf-8
    
    import requests
    from bs4 import BeautifulSoup
    import MySQLdb
    
    print('连接到mysql服务器...')
    db = MySQLdb.connect("127.0.0.1","xunrui","root.","123456")
    print('连接上了!')
    cursor = db.cursor()
    cursor.execute("DROP TABLE IF EXISTS COLOR")
    sql = """CREATE TABLE COLOR (
            Color CHAR(20) NOT NULL,
            Value CHAR(10),
            Style CHAR(50) )"""
    
    cursor.execute(sql)
    
    hdrs = {'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'}
    
    url = "http://html-color-codes.info/color-names/"
    
    r = requests.get(url, headers = hdrs)
    soup = BeautifulSoup(r.content.decode('gbk', 'ignore'), 'lxml')
    trs = soup.find_all('tr')   # 获取全部tr标签成为一个列表
    for tr in trs:              # 遍历列表里所有的tr标签单项
        style = tr.get('style') # 获取每个tr标签里的属性style
        tds = tr.find_all('td') # 将每个tr标签下的td标签获取为列表
        td = [x for x in tds]   # 获取的列表
        name = td[1].text.strip()       # 直接从列表里取值
        hex = td[2].text.strip()
        # print u'颜色: ' + name + u'颜色值: '+ hex + u'背景色样式: ' + style
        # print 'color: ' + name + '	value: '+ hex + '	style: ' + style
        insert_color = ("INSERT INTO COLOR(Color,Value,Style)" "VALUES(%s,%s,%s)")
        data_color = (name, hex, style)
        cursor.execute(insert_color, data_color)
        db.commit()
        # print '******完成此条插入!' 
    
    print '爬取数据并插入mysql数据库完成...'
  • 相关阅读:
    将来要干啥
    选新技术考虑点
    hdfs 创建一个新用户
    linux下实现mysql数据库定时备份
    PostgreSQL的安装和卸载,远程连接
    PostgreSQL语法
    【NiFi系列】1-基本介绍
    大数据相关资源网址
    MySQL主从复制配置
    MySQL设置免密登录
  • 原文地址:https://www.cnblogs.com/yszr/p/15028640.html
Copyright © 2011-2022 走看看