1. 程式人生 > >python第一個爬蟲的例子抓取數據到mysql,實測有數據

python第一個爬蟲的例子抓取數據到mysql,實測有數據

入mysql數據庫 nor gecko /usr png 支持 web local webkit

python3.5

先安裝庫或者擴展

1 requests第三方擴展庫

pip3 install requests

2 pymysql

pip3 install pymysql

3 lxml

pip3 install lxml

4 貼個代碼

#!/usr/bin/env python
# coding=utf-8

import requests
from bs4 import BeautifulSoup
import pymysql

print(連接到mysql服務器...)
db = pymysql.connect("localhost","root","root","python
") 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 + ‘\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 (爬取數據並插入mysql數據庫完成...)

5 運行這個代碼 ptyhon demo3.py

6 看看運行的結果

技術分享

7 數據庫裏面看看結果

技術分享

支持完成,成功,等會抓個別的例子 練練手

python第一個爬蟲的例子抓取數據到mysql,實測有數據