欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬取熱搜制作詞云

 更新時間:2022年01月25日 10:54:46   作者:Dead_Cicle  
這篇文章主要介紹了python爬取百度熱搜制作詞云,首先爬取百度熱搜,至少間隔1小時,存入文件,避免重復(fù)請求,如果本1小時有了不再請求,存入數(shù)據(jù)庫,供詞云包使用,爬取熱搜,具體流程請需要的小伙伴參考下面文章內(nèi)容

環(huán)境:win10,64位,mysql5.7數(shù)據(jù)庫,python3.9.7,ancod

邏輯流程:

  • 1、首先爬取百度熱搜,至少間隔1小時
  • 2、存入文件,避免重復(fù)請求,如果本1小時有了不再請求
  • 3、存入數(shù)據(jù)庫,供詞云包使用
  • 1、爬取熱搜,首先拿到url,使用的包urllib,有教程說urllib2python2的。
'''讀取頁面'''
def readhtml(self,catchUrl):
    catchUrl=self.catchUrl if not catchUrl else catchUrl
    response=urllib.request.urlopen(catchUrl)
    text=response.read().decode(self.bmcode)
    return text


這里在本類定義了幾個屬性,上述self的就是,這不是重點,繼續(xù),上述使用了三目運算符

'''生成時間'''
    def createTime(self):
        # 格式化成2016-03-20 11:45:39形式
        return time.strftime("%Y%m%d%H", time.localtime())
    '''寫入文件'''
    def write2file(self,text):
        fileName=self.writePosition+self.createTime()+'.txt'
        self.fileName=fileName
        print(fileName)
        #判斷路徑,不存在生成
        if not os.path.exists(self.writePosition):
            os.mkdir(self.writePosition )
        if os.path.exists(fileName):
            uuid_tools().printlog('已經(jīng)存在:{}'.format(fileName))
            return self.fileName
        mode='a' if os.path.exists(fileName) else 'w'
 
        with open(fileName,mode,encoding=self.bmcode) as f:
            f.write(text)
        print("寫入{} 完成".format(fileName))
        return self.fileName


這里每個小時生成一個文件名稱,避免了重復(fù),如果這一小時里已經(jīng)抓取過了,那么不再抓取了。

這里使用了日志(需導(dǎo)入日志包import logging):

    '''打印日志'''
    def printlog(self,loginfo):
        logging.basicConfig(level=logging.INFO)
        logging.info(loginfo)


獲取到內(nèi)容后,這里需要去掉<div>xxx</div>這個東西,還有個查看更多

'''去掉標簽'''
    def removeBq(self,content):
        pat=re.compile('>(.*?)<')
        str=''.join(pat.findall(content))
        str=str.replace(' 查看更多&gt; ','')
        return str
    '''輸出內(nèi)容'''
    def printContent(self, o,class_name):
        print(self.removeBq(str(o.find(class_=class_name))))


這里是beautifulsoup分析html格式的內(nèi)容:

'''測試獲取某個片段'''
    def readFilePd(self,fileName):
        #fileName='d:\\bdrs\\2021122010.txt'
        jt=open(fileName,'r',encoding=self.bmcode)
        try:
            content=jt.read()
            soup=BeautifulSoup(content,"html.parser")
            rs=RsBean()
            for k in soup.find_all('div',class_=rs.alldiv):
                print( str(k))
                # self.printContent(k,rs.sx)
                # self.printContent(k,rs.bt)
                # self.printContent(k,rs.ms)
                # self.printContent(k,rs.rszs)
        except  Exception as e:
            print('error:'+str(e))


最主要的是這一句:soup.find_all('div',class_='xxx')尤其這個橫杠,是該方法參數(shù),代表標簽的class名稱。

最后插入數(shù)據(jù)庫,

'''打開連接'''
    def open(self):
        self.db=pymysql.connect(host=self.host,port=3306,user=self.user,passwd=self.passwd,db=self.database)
        #創(chuàng)建游標
        self.cursor=self.db.cursor()
        #print("打開連接成功")
    #關(guān)閉
    def close(self):
        self.cursor.close()
        self.db.close()
        #print("close連接成功")
def execute(self,sql,list=[]):
        try:
            self.open()
            self.cursor.execute(sql,list)
            self.db.commit()
            print("execute successfully!")
        except Exception as e:
            self.db.rollback()
            print("Execute failure!",str(e))
        self.close()


封裝的代碼,后邊這樣使用:

'''讀取文件'''
    def readFile(self,fileName):
        #fileName='d:\\bdrs\\2021122010.txt'
        jt=open(fileName,'r',encoding=self.bmcode)
        try:
            content=jt.read()
            soup=BeautifulSoup(content,"html.parser")
            rs=RsBean()
            daoOper=OperateRsDao()
            rs_shijian=self.cutfilename(fileName)
            #先清空
            self.clearBefore(daoOper,rs_shijian)
            #讀取文件
            for k in soup.find_all('div',class_=rs.alldiv):
                #插入數(shù)據(jù)
                rs.rs_shijian=rs_shijian
                self.insertData(daoOper,k,rs)
        except  Exception as e:
            print('error:'+str(e))


最后詞云顯示:

if __name__ == '__main__':
    a=db_connect()
    sql='select id,shun_xu,biao_ti,miao_shu,reshou_zhishu,insert_time,rs_shijian from bdrs_one order by rs_shijian desc,shun_xu asc '
    result=a.select(sql)
    jieguo=''
    for m in result:
        print(m)
        jieguo+=m[2]
    #根據(jù)title做詞云
    CiYun().showImage(jieguo)


效果:

 可以看出最大的瓜是啥。

到此這篇關(guān)于python爬取百度熱搜制作詞云的文章就介紹到這了,更多相關(guān)python爬取熱搜制作詞云內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python在報表自動化的優(yōu)勢及實現(xiàn)流程

    Python在報表自動化的優(yōu)勢及實現(xiàn)流程

    本文利用Python實現(xiàn)報表自動化,通過介紹環(huán)境設(shè)置、數(shù)據(jù)收集和準備、報表生成以及自動化流程,展示Python的靈活性和豐富的生態(tài)系統(tǒng)在報表自動化中的卓越表現(xiàn),從設(shè)置虛擬環(huán)境到使用Pandas和Matplotlib處理數(shù)據(jù),到借助APScheduler實現(xiàn)定期自動化,每個步驟都得到詳盡闡述
    2023-12-12
  • Python中的urllib模塊使用詳解

    Python中的urllib模塊使用詳解

    這篇文章主要介紹了Python中的urllib模塊使用詳解,是Python入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-07-07
  • python利用dlib獲取人臉的68個landmark

    python利用dlib獲取人臉的68個landmark

    這篇文章主要介紹了python利用dlib獲取人臉的68個landmark,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • 利用Python實現(xiàn)Windows下的鼠標鍵盤模擬的實例代碼

    利用Python實現(xiàn)Windows下的鼠標鍵盤模擬的實例代碼

    本篇文章主要介紹了利用Python實現(xiàn)Windows下的鼠標鍵盤模擬的實例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • python基礎(chǔ)教程之五種數(shù)據(jù)類型詳解

    python基礎(chǔ)教程之五種數(shù)據(jù)類型詳解

    這篇文章主要介紹了python基礎(chǔ)教程之五種數(shù)據(jù)類型詳解的相關(guān)資料,這里對Python 的數(shù)據(jù)類型進行了詳細介紹,需要的朋友可以參考下
    2017-01-01
  • 淺談numpy數(shù)組的幾種排序方式

    淺談numpy數(shù)組的幾種排序方式

    這篇文章主要介紹了淺談numpy數(shù)組的幾種排序方式,涉及對numpy的簡單介紹和創(chuàng)建數(shù)組的方式,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • python訪問sqlserver示例

    python訪問sqlserver示例

    這篇文章主要介紹了python訪問sqlserver示例,需要的朋友可以參考下
    2014-02-02
  • python爬蟲線程池案例詳解(梨視頻短視頻爬取)

    python爬蟲線程池案例詳解(梨視頻短視頻爬取)

    這篇文章主要介紹了python爬蟲線程池案例詳解(梨視頻短視頻爬取),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Django 表單模型選擇框如何使用分組

    Django 表單模型選擇框如何使用分組

    這篇文章主要介紹了Django 表單模型選擇框如何使用分組,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 幾個適合python初學者的簡單小程序,看完受益匪淺!(推薦)

    幾個適合python初學者的簡單小程序,看完受益匪淺!(推薦)

    這篇文章主要介紹了幾個適合python初學者的簡單小程序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04

最新評論