爬取知乎熱榜


1.打開網頁https://tophub.today/n/mproPpoq6O

 

 

2.按Ctrl+U 打開網頁源代碼

3.尋找爬取內容

4.

import requests
import pandas as pd
from bs4 import BeautifulSoup
import pandas as pd
lst=[]
url='https://tophub.today/n/mproPpoq6O'
def get(url):
    try:
        headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'}
        url = requests.get(url,timeout = 30,headers=headers)
        url.raise_for_status()
        url.encoding='utf-8'
        return url.text 
    except:
        return "產生異常"
def create(lst,html,num):
        soup=BeautifulSoup(html,'html.parser'
        b=soup.find_all('span',class_='e')
        print('{:^10}\t{:^30}\t{:^10}'.format('排名', '標題', '熱度'))
        for i in range(num):
            print('{:^10}\t{:^30}\t{:^10}'.format(i+1, a[i+50].string, b[i+50].string)
            lst.append([i+1,a[i+50].string,b[i+50].string]
html=get(url)
create(lst,html,10)
df=pd.DataFrame(lst,columns=['排名','標題','熱度'])
ZHHot='D:\Python\知乎熱搜'
df.to_excel(ZHHot)

5.爬取數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM