日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python网站数据写入mysql_python网络爬虫抓取动态网页并将数据存入数据库MySQL

發(fā)布時(shí)間:2023/12/2 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python网站数据写入mysql_python网络爬虫抓取动态网页并将数据存入数据库MySQL 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡述

以下的代碼是使用python實(shí)現(xiàn)的網(wǎng)絡(luò)爬蟲,抓取動(dòng)態(tài)網(wǎng)頁?http://hb.qq.com/baoliao/?。此網(wǎng)頁中的最新、精華下面的內(nèi)容是由JavaScript動(dòng)態(tài)生成的。審查網(wǎng)頁元素與網(wǎng)頁源碼是不同。

以上是網(wǎng)頁源碼

以上是審查網(wǎng)頁元素

所以此處不能簡單的使用正則表達(dá)式來獲取內(nèi)容。

以下是完整的獲取內(nèi)容并存儲(chǔ)到數(shù)據(jù)庫的思路及源碼。

實(shí)現(xiàn)思路:

抓取實(shí)際訪問的動(dòng)態(tài)頁面的url?–?使用正則表達(dá)式獲取需要的內(nèi)容?–?解析內(nèi)容?–?存儲(chǔ)內(nèi)容

以上部分過程文字解釋:

抓取實(shí)際訪問的動(dòng)態(tài)頁面的url:

在火狐瀏覽器中,右鍵打開插件 使用**firebug審查元素** *(沒有這項(xiàng)的,要安裝firebug插件),找到并打開**網(wǎng)絡(luò)(NET)**標(biāo)簽頁。重新加載網(wǎng)頁,獲得網(wǎng)頁的響應(yīng)信息,包括連接地址。每個(gè)連接地址都可以在瀏覽器中打開。本網(wǎng)站的動(dòng)態(tài)網(wǎng)頁訪問地址是:

http://baoliao.hb.qq.com/api/report/NewIndexReportsList/cityid/18/num/20/pageno/1?callback=jQuery183019859437816181613_1440723895018&_=1440723895472

正則表達(dá)式:

正則表達(dá)式的使用有兩種思路,可以參考個(gè)人有關(guān)其簡述:python實(shí)現(xiàn)簡單爬蟲以及正則表達(dá)式簡述

更多的細(xì)節(jié)介紹可以參考網(wǎng)上資料,搜索關(guān)鍵詞: 正則表達(dá)式 python

json:

參考網(wǎng)上有關(guān)json的介紹,搜索關(guān)鍵詞: json python

存儲(chǔ)到數(shù)據(jù)庫:

參考網(wǎng)上的使用介紹,搜索關(guān)鍵詞: 1,mysql 2,mysql python

源碼及注釋

注意:使用python的版本是 2.7

#!/usr/bin/python#指明編碼#-*- coding: UTF-8 -*-

#導(dǎo)入python庫

importurllibimporturllib2importreimportMySQLdbimportjson#定義爬蟲類

classcrawl1:def getHtml(self,url=None):#代理

user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"header={"User-Agent":user_agent}

request=urllib2.Request(url,headers=header)

response=urllib2.urlopen(request)

html=response.read()returnhtmldefgetContent(self,html,reg):

content=re.findall(html, reg, re.S)returncontent#連接數(shù)據(jù)庫 mysql

defconnectDB(self):

host="192.168.85.21"dbName="test1"user="root"password="123456"

#此處添加charset='utf8'是為了在數(shù)據(jù)庫中顯示中文,此編碼必須與數(shù)據(jù)庫的編碼一致

db=MySQLdb.connect(host,user,password,dbName,charset='utf8')returndb

cursorDB=db.cursor()returncursorDB#創(chuàng)建表,SQL語言。CREATE TABLE IF NOT EXISTS 表示:表createTableName不存在時(shí)就創(chuàng)建

defcreatTable(self,createTableName):

createTableSql="CREATE TABLE IF NOT EXISTS"+ createTableName+"(time VARCHAR(40),title VARCHAR(100),text VARCHAR(40),clicks VARCHAR(10))"DB_create=self.connectDB()

cursor_create=DB_create.cursor()

cursor_create.execute(createTableSql)

DB_create.close()print 'creat table'+createTableName+'successfully'

returncreateTableName#數(shù)據(jù)插入表中

definserttable(self,insertTable,insertTime,insertTitle,insertText,insertClicks):

insertContentSql="INSERT INTO"+insertTable+"(time,title,text,clicks)VALUES(%s,%s,%s,%s)"

#insertContentSql="INSERT INTO "+insertTable+"(time,title,text,clicks)VALUES("+insertTime+" , "+insertTitle+" , "+insertText+" , "+insertClicks+")"

DB_insert=self.connectDB()

cursor_insert=DB_insert.cursor()

cursor_insert.execute(insertContentSql,(insertTime,insertTitle,insertText,insertClicks))

DB_insert.commit()

DB_insert.close()print 'inert contents to'+insertTable+'successfully'url="http://baoliao.hb.qq.com/api/report/NewIndexReportsList/cityid/18/num/20/pageno/1?callback=jQuery183019859437816181613_1440723895018&_=1440723895472"

#正則表達(dá)式,獲取js,時(shí)間,標(biāo)題,文本內(nèi)容,點(diǎn)擊量(瀏覽次數(shù))

reg_jason=r'.*?jQuery.*?\((.*)\)'reg_time=r'.*?"create_time":"(.*?)"'reg_title=r'.*?"title":"(.*?)".*?'reg_text=r'.*?"content":"(.*?)".*?'reg_clicks=r'.*?"counter_clicks":"(.*?)"'

#實(shí)例化crawl()對(duì)象

crawl=crawl1()

html=crawl.getHtml(url)

html_jason=re.findall(reg_jason, html, re.S)

html_need=json.loads(html_jason[0])printlen(html_need)print len(html_need['data']['list'])

table=crawl.creatTable('yh1')for i in range(len(html_need['data']['list'])):

creatTime=html_need['data']['list'][i]['create_time']

title=html_need['data']['list'][i]['title']

content=html_need['data']['list'][i]['content']

clicks=html_need['data']['list'][i]['counter_clicks']

crawl.inserttable(table,creatTime,title,content,clicks)

總結(jié)

以上是生活随笔為你收集整理的python网站数据写入mysql_python网络爬虫抓取动态网页并将数据存入数据库MySQL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。