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

如何使用python爬取知乎熱榜Top50數(shù)據(jù)

 更新時間:2021年09月27日 09:45:30   作者:小狐貍夢想去童話鎮(zhèn)  
主要是爬取知乎熱榜的問題及點贊數(shù)比較高的答案,通過requests請求庫進行爬取,對大家的學習或工作具有一定的價值,需要的朋友可以參考下

1、導入第三方庫

import urllib.request,urllib.error  #請求網(wǎng)頁
from bs4 import BeautifulSoup  # 解析數(shù)據(jù)
import sqlite3  # 導入數(shù)據(jù)庫
import re # 正則表達式
import time # 獲取當前時間

2、程序的主函數(shù)

def main():
    # 聲明爬取網(wǎng)頁
    baseurl = "https://www.zhihu.com/hot"
    # 爬取網(wǎng)頁
    datalist = getData(baseurl)
    #保存數(shù)據(jù)
    dbname = time.strftime("%Y-%m-%d", time.localtime()) # 
    dbpath = "zhihuTop50  " + dbname
    saveData(datalist,dbpath)

3、正則表達式匹配數(shù)據(jù)

#正則表達式
findlink = re.compile(r'<a class="css-hi1lih" href="(.*?)" rel="external nofollow"  rel="external nofollow" ') #問題鏈接
findid = re.compile(r'<div class="css-blkmyu">(.*?)</div>') #問題排名
findtitle = re.compile(r'<h1 class="css-3yucnr">(.*?)</h1>') #問題標題
findintroduce = re.compile(r'<div class="css-1o6sw4j">(.*?)</div>') #簡要介紹
findscore = re.compile(r'<div class="css-1iqwfle">(.*?)</div>') #熱門評分
findimg = re.compile(r'<img class="css-uw6cz9" src="(.*?)"/>') #文章配圖

4、程序運行結果

在這里插入圖片描述

在這里插入圖片描述

5、程序源代碼

import urllib.request,urllib.error
from bs4 import BeautifulSoup
import sqlite3
import re
import time

def main():
    # 聲明爬取網(wǎng)頁
    baseurl = "https://www.zhihu.com/hot"
    # 爬取網(wǎng)頁
    datalist = getData(baseurl)
    #保存數(shù)據(jù)
    dbname = time.strftime("%Y-%m-%d", time.localtime())
    dbpath = "zhihuTop50  " + dbname
    saveData(datalist,dbpath)
    print()
#正則表達式
findlink = re.compile(r'<a class="css-hi1lih" href="(.*?)" rel="external nofollow"  rel="external nofollow" ') #問題鏈接
findid = re.compile(r'<div class="css-blkmyu">(.*?)</div>') #問題排名
findtitle = re.compile(r'<h1 class="css-3yucnr">(.*?)</h1>') #問題標題
findintroduce = re.compile(r'<div class="css-1o6sw4j">(.*?)</div>') #簡要介紹
findscore = re.compile(r'<div class="css-1iqwfle">(.*?)</div>') #熱門評分
findimg = re.compile(r'<img class="css-uw6cz9" src="(.*?)"/>') #文章配圖

def getData(baseurl):
    datalist = []
    html = askURL(baseurl)
    # print(html)

    soup = BeautifulSoup(html,'html.parser')
    for item in soup.find_all('a',class_="css-hi1lih"):
        # print(item)
        data = []
        item = str(item)

        Id = re.findall(findid,item)
        if(len(Id) == 0):
            Id = re.findall(r'<div class="css-mm8qdi">(.*?)</div>',item)[0]
        else: Id = Id[0]
        data.append(Id)
        # print(Id)

        Link = re.findall(findlink,item)[0]
        data.append(Link)
        # print(Link)

        Title = re.findall(findtitle,item)[0]
        data.append(Title)
        # print(Title)

        Introduce = re.findall(findintroduce,item)
        if(len(Introduce) == 0):
            Introduce = " "
        else:Introduce = Introduce[0]
        data.append(Introduce)
        # print(Introduce)

        Score = re.findall(findscore,item)[0]
        data.append(Score)
        # print(Score)

        Img = re.findall(findimg,item)
        if (len(Img) == 0):
            Img = " "
        else: Img = Img[0]
        data.append(Img)
        # print(Img)
        datalist.append(data)
    return datalist
def askURL(baseurl):
    # 設置請求頭
    head = {
        # "User-Agent": "Mozilla/5.0 (Windows NT 10.0;Win64;x64) AppleWebKit/537.36(KHTML, likeGecko) Chrome/80.0.3987.163Safari/537.36"
        "User-Agent": "Mozilla / 5.0(iPhone;CPUiPhoneOS13_2_3likeMacOSX) AppleWebKit / 605.1.15(KHTML, likeGecko) Version / 13.0.3Mobile / 15E148Safari / 604.1"
    }
    request = urllib.request.Request(baseurl, headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
        # print(html)
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
    return html
    print()
def saveData(datalist,dbpath):
    init_db(dbpath)
    conn = sqlite3.connect(dbpath)
    cur = conn.cursor()

    for data in datalist:
        sql = '''
        insert into Top50(
        id,info_link,title,introduce,score,img)
        values("%s","%s","%s","%s","%s","%s")'''%(data[0],data[1],data[2],data[3],data[4],data[5])
        print(sql)
        cur.execute(sql)
        conn.commit()
    cur.close()
    conn.close()
def init_db(dbpath):
    sql = '''
    create table Top50
    (
    id integer primary key autoincrement,
    info_link text,
    title text,
    introduce text,
    score text,
    img text
    )
    '''
    conn = sqlite3.connect(dbpath)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()

if __name__ =="__main__":
    main()

到此這篇關于如何使用python爬取知乎熱榜Top50數(shù)據(jù)的文章就介紹到這了,更多相關python 爬取知乎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python3.x如何向mysql存儲圖片并顯示

    python3.x如何向mysql存儲圖片并顯示

    這篇文章主要介紹了python3.x如何向mysql存儲圖片并顯示問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 如何使用?Python為你的在線會議創(chuàng)建一個假的攝像頭

    如何使用?Python為你的在線會議創(chuàng)建一個假的攝像頭

    這篇文章主要介紹了使用?Python為你的在線會議創(chuàng)建一個假的攝像頭,在?Python?的幫助下,不再強制開啟攝像頭,將向你展示如何為你的在線會議創(chuàng)建一個假的攝像頭,需要的朋友可以參考下
    2022-08-08
  • Python 實現(xiàn)局域網(wǎng)遠程屏幕截圖案例

    Python 實現(xiàn)局域網(wǎng)遠程屏幕截圖案例

    這篇文章主要介紹了Python 實現(xiàn)局域網(wǎng)遠程屏幕截圖案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 基于Python自制視覺桌上冰球小游戲

    基于Python自制視覺桌上冰球小游戲

    這篇文章主要和大家分享一下如何使用?mediapipe+opencv?制作桌上冰球的交互式小游戲,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-04-04
  • Django框架cookie和session方法及參數(shù)設置

    Django框架cookie和session方法及參數(shù)設置

    這篇文章主要為大家介紹了Django框架cookie和session參數(shù)設置及介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • 利用Python和C++實現(xiàn)解析gltf文件

    利用Python和C++實現(xiàn)解析gltf文件

    gltf是類似于stl、obj、ply等常見的3D對象存儲格式,它被設計出來是為了便于渲染的數(shù)據(jù)轉(zhuǎn)換和傳輸,本文為大家介紹了使用Python和C++解析gltf文件的方法,感興趣的可以了解下
    2023-09-09
  • Python3第三方模塊之Pillow模塊的使用詳解

    Python3第三方模塊之Pillow模塊的使用詳解

    這篇文章主要介紹了Python3第三方模塊之Pillow模塊的使用詳解,在 pillow之前處理圖形的庫莫過于PIL,但是它支持到python2.7,年久失修,于是一群志愿者在PIL的基礎上常見了pillow,支持python3,又豐富和功能特性,需要的朋友可以參考下
    2023-10-10
  • Python項目實戰(zhàn)之使用Django框架實現(xiàn)支付寶付款功能

    Python項目實戰(zhàn)之使用Django框架實現(xiàn)支付寶付款功能

    這篇文章主要介紹了Python項目實戰(zhàn)之使用Django框架實現(xiàn)支付寶付款功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Python實現(xiàn)控制手機電腦拍照并自動發(fā)送郵箱

    Python實現(xiàn)控制手機電腦拍照并自動發(fā)送郵箱

    這篇文章主要介紹了如何實現(xiàn)利用Python控制手機電腦拍照并自動發(fā)送郵箱,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起動手試一試
    2022-02-02
  • Tensorflow?2.4加載處理圖片的三種方式詳解

    Tensorflow?2.4加載處理圖片的三種方式詳解

    這篇文章主要為大家介紹了Tensorflow?2.4加載處理圖片的三種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論

五指山市| 类乌齐县| 锡林郭勒盟| 宜丰县| 保山市| 宁津县| 桃园市| 桃园市| 宁津县| 沙湾县| 酉阳| 东平县| 辉县市| 赞皇县| 新巴尔虎左旗| 天门市| 新野县| 阿荣旗| 英山县| 启东市| 贵阳市| 东至县| 灵寿县| 曲水县| 阜宁县| 镇赉县| 平原县| 海宁市| 常熟市| 苏尼特左旗| 金坛市| 宜川县| 塘沽区| 同仁县| 新邵县| 团风县| 嘉黎县| 句容市| 田林县| 岳普湖县| 博客|