国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python爬取數(shù)據(jù)并寫入MySQL數(shù)據(jù)庫(kù)的實(shí)例

2020-02-15 21:57:23
字體:
供稿:網(wǎng)友

首先我們來爬取 http://html-color-codes.info/color-names/ 的一些數(shù)據(jù)。

按 F12 或 ctrl+u 審查元素,結(jié)果如下:

結(jié)構(gòu)很清晰簡(jiǎn)單,我們就是要爬 tr 標(biāo)簽里面的 style 和 tr 下幾個(gè)并列的 td 標(biāo)簽,下面是爬取的代碼:

#!/usr/bin/env python# coding=utf-8import requestsfrom bs4 import BeautifulSoupimport MySQLdbprint('連接到mysql服務(wù)器...')db = MySQLdb.connect("localhost","hp","Hp12345.","TESTDB")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標(biāo)簽成為一個(gè)列表for tr in trs:    # 遍歷列表里所有的tr標(biāo)簽單項(xiàng) style = tr.get('style') # 獲取每個(gè)tr標(biāo)簽里的屬性style tds = tr.find_all('td') # 將每個(gè)tr標(biāo)簽下的td標(biāo)簽獲取為列表 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 + '/tvalue: '+ hex + '/tstyle: ' + 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 '爬取數(shù)據(jù)并插入mysql數(shù)據(jù)庫(kù)完成...'

運(yùn)行結(jié)果:

$ mysql -u hp -pEnter password: Welcome to the MySQL monitor. Commands end with ; or /g.Your MySQL connection id is 28Server version: 5.7.17 MySQL Community Server (GPL)Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.mysql> use TESTDBReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from COLOR;+----------------------+--------+----------------------------------------+| Color    | Value | Style         |+----------------------+--------+----------------------------------------+| IndianRed   | CD5C5C | background-color:indianred;   || LightCoral   | F08080 | background-color:lightcoral;   || Salmon    | FA8072 | background-color:salmon;    || DarkSalmon   | E9967A | background-color:darksalmon;   || LightSalmon   | FFA07A | background-color:lightsalmon;   || Crimson    | DC143C | background-color:crimson;    || Red     | FF0000 | background-color:red;     || FireBrick   | B22222 | background-color:fireBrick;   || DarkRed    | 8B0000 | background-color:darkred;    || Pink     | FFC0CB | background-color:pink;     || LightPink   | FFB6C1 | background-color:lightpink;   || HotPink    | FF69B4 | background-color:hotpink;    || DeepPink    | FF1493 | background-color:deeppink;    |...| AntiqueWhite   | FAEBD7 | background-color:antiquewhite;   || Linen    | FAF0E6 | background-color:linen;    || LavenderBlush  | FFF0F5 | background-color:lavenderblush;  || MistyRose   | FFE4E1 | background-color:mistyrose;   || Gainsboro   | DCDCDC | background-color:gainsboro;   || LightGrey   | D3D3D3 | background-color:lightgrey;   || Silver    | C0C0C0 | background-color:silver;    || DarkGray    | A9A9A9 | background-color:darkgray;    || Gray     | 808080 | background-color:gray;     || DimGray    | 696969 | background-color:dimgray;    || LightSlateGray  | 778899 | background-color:lightslategray;  || SlateGray   | 708090 | background-color:slategray;   || DarkSlateGray  | 2F4F4F | background-color:darkslategray;  || Black    | 000000 | background-color:black;    |+----------------------+--------+----------------------------------------+143 rows in set (0.00 sec)            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 雅江县| 吉林省| 浦江县| 饶平县| 雷波县| 临桂县| 麻栗坡县| 台前县| 望江县| 昭苏县| 丹江口市| 山阴县| 荔浦县| 辽中县| 东丰县| 和田市| 尤溪县| 海阳市| 廊坊市| 丰镇市| 达日县| 诏安县| 兴宁市| 简阳市| 江源县| 青铜峡市| 罗城| 无极县| 神木县| 巧家县| 紫金县| 惠安县| 金昌市| 喀什市| 鄂温| 谷城县| 渝中区| 股票| 临猗县| 丰镇市| 甘谷县|