最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細(xì)教程(一看就會(huì))

 更新時(shí)間:2022年06月11日 16:37:02   作者:hippoDocker  
使用爬蟲爬數(shù)據(jù),總要涉及到數(shù)據(jù)持久化,也就是數(shù)據(jù)存儲(chǔ)的問題,下面這篇文章主要給大家介紹了關(guān)于Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細(xì)教程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1.簡介介紹

-網(wǎng)絡(luò)爬蟲(又稱為網(wǎng)頁蜘蛛,網(wǎng)絡(luò)機(jī)器人,在FOAF社區(qū)中間,更經(jīng)常的稱為網(wǎng)頁追逐者),是一種按照一定的規(guī)則,自動(dòng)地抓取萬維網(wǎng)信息的程序或者腳本。另外一些不常使用的名字還有螞蟻、自動(dòng)索引、模擬程序或者蠕蟲。
-一般在瀏覽器上可以獲取到的,通過爬蟲也可以獲取到,常見的爬蟲語言有PHP,JAVA,C#,C++,Python,為啥我們經(jīng)常聽到說的都是Python爬蟲,這是因?yàn)閜ython爬蟲比較簡單,功能比較齊全。

2.Xpath獲取頁面信息

通過Xpath進(jìn)行爬蟲就是獲取到頁面html后通過路徑的表達(dá)式來選取標(biāo)簽節(jié)點(diǎn),沿著路徑選取需要爬取的數(shù)據(jù)。

Xpath常用表達(dá)式:

表達(dá)式描述
/從根節(jié)點(diǎn)選取(取子節(jié)點(diǎn))
//選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn)
.選取當(dāng)前節(jié)點(diǎn)。
選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。
@選取屬性
*表示任意內(nèi)容(通配符)
|運(yùn)算符可以選取多個(gè)路徑

Xpath常用函數(shù):

函數(shù)用法解釋
startswith()xpath(‘//div[starts-with(@id,”celent”)]‘)#選取id值以celent開頭的div節(jié)點(diǎn)
contains()xpath(‘//div[contains(@id,”celent”)]‘)#選取id值包含celent的div節(jié)點(diǎn)
and()xpath(‘//div[contains(@id,”celent”) and contains(@id,”in”)]‘)#選取id值包含celent的div節(jié)點(diǎn)
text()_.xpath(’./div/div[4]/a/em/text()’)#選取em標(biāo)簽下文本內(nèi)容

Xpath實(shí)操解析:

# 案例1
# //為從當(dāng)前html中選取節(jié)點(diǎn);[@class="c1text1"]為獲取所有的class為c1text1的節(jié)點(diǎn);/h1[1]為選取的節(jié)點(diǎn)下的第一個(gè)h1節(jié)點(diǎn),如果沒有[1]則是獲取所有的,可以通過循環(huán)進(jìn)行獲取數(shù)據(jù)
etreeHtml.xpath('//*[@class="c1text1"]/h1[1]/text()')

# 案例2
#//為從當(dāng)前html中選取節(jié)點(diǎn);[@class="c1text1"]為獲取所有的class為c1text1的節(jié)點(diǎn);/a為獲取當(dāng)前節(jié)點(diǎn)下的所有a標(biāo)簽節(jié)點(diǎn),得到一個(gè)ObjectList;通過for循環(huán)獲取里面每個(gè)標(biāo)簽數(shù)據(jù),./@src為獲取當(dāng)前節(jié)點(diǎn)的src屬性值
etreeHtml2 = etreeHtml.xpath('//*[@class="c1text1"]/a')
for _ in etreeHtml2: 
	etreeHtml.xpath(./@src)

3.通過Xpath爬蟲實(shí)操

本次實(shí)例以爬取我的CSDN文章列表信息保存到數(shù)據(jù)庫為案列

3-1.獲取xpath

通過F12打開開發(fā)者模式,點(diǎn)擊左上角圖標(biāo)可參考下圖,選擇需要爬取數(shù)據(jù)的容器,在右邊選擇復(fù)制選擇xpath就可以得到xpath路徑了(//*[@id=“userSkin”]/div[2]/div/div[2]/div[1]/div[2]/div/div);

完整代碼展示:

# 導(dǎo)入需要的庫
import requests
from lxml import etree
import pymysql


# 文章詳情信息類
class articleData():
    def __init__(self, title, abstract, path,date):
        self.title = title #文章名稱
        self.abstract = abstract #文章摘要
        self.path = path #文章路徑
        self.date = date #發(fā)布時(shí)間


    def to_string(self):
        print("文章名稱:"+self.title
              +";文章摘要:"+self.abstract
              +";文章路徑:"+self.path
              +";發(fā)布時(shí)間:"+self.date)

#保存狗狗詳情數(shù)據(jù)
#保存數(shù)據(jù)
def saveData(DataObject):
    count = pymysql.connect(
        host='xx.xx.xx.xx',  # 數(shù)據(jù)庫地址
        port=3306,  # 數(shù)據(jù)庫端口
        user='xxxxx',  # 數(shù)據(jù)庫賬號(hào)
        password='xxxxxx',  # 數(shù)據(jù)庫密碼
        db='xxxxxxx'  # 數(shù)據(jù)庫名
    )
    # 創(chuàng)建數(shù)據(jù)庫對(duì)象
    db = count.cursor()
    # 寫入sql
    # print("寫入數(shù)據(jù):"+DataObject.to_string())
    sql = f"insert into article_detail(title,abstract,alias,path,date) " \
          f"values ('{DataObject.title}','{DataObject.abstract}','{DataObject.path}','{DataObject.date}')"
    # 執(zhí)行sql
    print(sql)
    db.execute(sql)
    # 保存修改內(nèi)容
    count.commit()
    db.close()

# 爬取數(shù)據(jù)的方向
def getWebData():
    # 網(wǎng)站頁面路徑
    url = "https://blog.csdn.net/BadBoyxiaolin?spm=1000.2115.3001.5343"
    # 請(qǐng)求頭,模擬瀏覽器請(qǐng)求
    header = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36"
    }
    # 獲取頁面所有節(jié)點(diǎn)代碼
    html = requests.get(url=url, headers=header)
    # 打印頁面代碼查看
    # print(html.text)
    # 如果亂碼可以設(shè)置編碼格式
    # html.encoding = 'gb2312'
    # 通過xpath獲取數(shù)據(jù)對(duì)應(yīng)節(jié)點(diǎn)
    etreeHtml = etree.HTML(html.text)
    dataHtml = etreeHtml.xpath('//*[@class="mainContent"]/div/div/div')
    # 循環(huán)獲取數(shù)據(jù)
    for _ in dataHtml:
        # ''.join()是將內(nèi)容轉(zhuǎn)換為字符串可以后面接replace數(shù)據(jù)進(jìn)行處理
        title = ''.join(_.xpath('./article/a/div[1]/h4/text()'))#文章標(biāo)題
        abstract = ''.join(_.xpath('./article/a/div[2]/text()'))#文章摘要
        path = ''.join(_.xpath('./article/a/@href'))#文章路徑
        date = ''.join(_.xpath('./article/a/div[3]/div/div[2]/text()')).replace(' ','').replace('·','').replace('發(fā)布博客','')#發(fā)布時(shí)間
        #初始化文章類數(shù)據(jù)
        article_data = articleData(title,abstract,path,date)
        article_data.to_string() #打印數(shù)據(jù)看看是否對(duì)
        #保存數(shù)據(jù)到數(shù)據(jù)庫
        # saveData(article_data)

if __name__ == "__main__":
    getWebData()

總結(jié)

到此這篇關(guān)于Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的文章就介紹到這了,更多相關(guān)Python爬蟲數(shù)據(jù)保存到數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

绩溪县| 广德县| 蛟河市| 墨玉县| 肥东县| 年辖:市辖区| 教育| 辉县市| 乐至县| 永仁县| 屏边| 泊头市| 涡阳县| 邵阳县| 浠水县| 东兰县| 来凤县| 东莞市| 新密市| 龙海市| 景德镇市| 鱼台县| 万载县| 常州市| 界首市| 河津市| 横山县| 鹤山市| 苏州市| 堆龙德庆县| 朝阳市| 手机| 秦安县| 新田县| 容城县| 大关县| 东明县| 大渡口区| 栖霞市| 于田县| 枣强县|