濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > python爬蟲今日熱榜數(shù)據(jù)到txt文件的源碼

python爬蟲今日熱榜數(shù)據(jù)到txt文件的源碼

熱門標(biāo)簽:qt百度地圖標(biāo)注 正安縣地圖標(biāo)注app 電銷機(jī)器人系統(tǒng)廠家鄭州 400電話申請(qǐng)資格 地圖地圖標(biāo)注有嘆號(hào) 遼寧智能外呼系統(tǒng)需要多少錢 舉辦過(guò)冬奧會(huì)的城市地圖標(biāo)注 螳螂科技外呼系統(tǒng)怎么用 阿里電話機(jī)器人對(duì)話

今日熱榜:https://tophub.today/

爬取數(shù)據(jù)及保存格式:

爬取后保存為.txt文件:

部分內(nèi)容:


源碼及注釋:

import requests
from bs4 import BeautifulSoup

def download_page(url):
  headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}
  try:
    r = requests.get(url,timeout = 30,headers=headers)
    return r.text
  except:
    return "please inspect your url or setup"


def get_content(html,tag):
  output = """  排名:{}\n  標(biāo)題:{} \n  熱度:{}\n  鏈接:{}\n  ------------\n"""
  output2 = """平臺(tái):{}  榜單類型:{}  最近更新:{}\n------------\n"""
  num=[]
  title=[]
  hot=[]
  href=[]
  soup = BeautifulSoup(html, 'html.parser')
  con = soup.find('div',attrs={'class':'bc-cc'})
  con_list = con.find_all('div', class_="cc-cd")
  for i in con_list: 
    author = i.find('div', class_='cc-cd-lb').get_text() # 獲取平臺(tái)名字
    time = i.find('div', class_='i-h').get_text() # 獲取最近更新
    link = i.find('div', class_='cc-cd-cb-l').find_all('a') # 獲取所有鏈接 
    gender = i.find('span', class_='cc-cd-sb-st').get_text() # 獲取類型 
    save_txt(tag,output2.format(author, gender,time))
    for k in link:
      href.append(k['href'])
      num.append(k.find('span', class_='s').get_text())
      title.append(str(k.find('span', class_='t').get_text()))
      hot.append(str(k.find('span', class_='e').get_text()))
    for h in range(len(num)): 
      save_txt(tag,output.format(num[h], title[h], hot[h], href[h]))


def save_txt(tag,*args):
  for i in args:
    with open(tag+'.txt', 'a', encoding='utf-8') as f:
      f.write(i)


def main():
  #   綜合  科技  娛樂(lè)  社區(qū)    購(gòu)物   財(cái)經(jīng)
  page=['news','tech','ent','community','shopping','finance']
  for tag in page:
    url = 'https://tophub.today/c/{}'.format(tag)
    html = download_page(url)
    get_content(html,tag)

if __name__ == '__main__':
  main()

到此這篇關(guān)于python爬蟲今日熱榜數(shù)據(jù)到txt文件的源碼的文章就介紹到這了,更多相關(guān)python爬蟲今日熱榜數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python爬蟲爬取全球疫情數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的步驟
  • python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析
  • python實(shí)現(xiàn)scrapy爬蟲每天定時(shí)抓取數(shù)據(jù)的示例代碼
  • 詳解Python之Scrapy爬蟲教程N(yùn)BA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫(kù)
  • python爬蟲實(shí)現(xiàn)爬取同一個(gè)網(wǎng)站的多頁(yè)數(shù)據(jù)的實(shí)例講解
  • python爬蟲利用selenium實(shí)現(xiàn)自動(dòng)翻頁(yè)爬取某魚數(shù)據(jù)的思路詳解
  • python爬蟲分布式獲取數(shù)據(jù)的實(shí)例方法
  • python爬蟲爬取網(wǎng)頁(yè)數(shù)據(jù)并解析數(shù)據(jù)
  • Python爬蟲實(shí)例——爬取美團(tuán)美食數(shù)據(jù)
  • Python爬蟲數(shù)據(jù)的分類及json數(shù)據(jù)使用小結(jié)

標(biāo)簽:合肥 信陽(yáng) 昭通 阜新 濟(jì)源 興安盟 隨州 淘寶好評(píng)回訪

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python爬蟲今日熱榜數(shù)據(jù)到txt文件的源碼》,本文關(guān)鍵詞  python,爬蟲,今日,熱榜,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python爬蟲今日熱榜數(shù)據(jù)到txt文件的源碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于python爬蟲今日熱榜數(shù)據(jù)到txt文件的源碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    交口县| 延安市| 施秉县| 龙游县| 山东省| 乳山市| 西乌| 彭州市| 嘉鱼县| 福州市| 饶阳县| 韩城市| 驻马店市| 望奎县| 金沙县| 三门县| 重庆市| 阿拉善盟| 东乡族自治县| 成都市| 马尔康县| 泾阳县| 涿州市| 锦州市| 辽阳县| 新巴尔虎左旗| 开化县| 大石桥市| 平凉市| 金堂县| 江阴市| 长兴县| 循化| 西林县| 定襄县| 韩城市| 凤城市| 肥城市| 叶城县| 富蕴县| 花莲市|