Python爬蟲實戰:爬取首都醫科大學文章
阿新 • • 發佈:2020-08-22
前言
本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯絡我們以作處理。
作者:人廚子
概述
爬蟲專案實踐
目標:首都醫科大學官網新發文章
步驟
- 請求網址
- 通過正則表示式提取資料
- 分析資料
程式碼
# 匯入模組 # 用於請求網址 import requests # 用於解析網頁原始碼 from bs4 import BeautifulSoup # 用於正則 import re # 目標網址 # 設定頁數,提取10頁的資料 page=[''] for i in range(1,10,1): page.append(i)# 儲存檔案 with open(r'ccmu.csv','a',encoding='utf-8') as f: for i in page: url= 'http://www.ccmu.edu.cn/zxkylw_12912/index'+str(i)+'.htm' # 必要時新增header請求頭,防止反爬攔截 headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0' }# 目標網址請求方式為get resp=requests.get(url) html = resp.content.decode('utf-8') # 解析html soup = BeautifulSoup(html,'html.parser') # 找到最近發表的sci論文 # 使用find和find_all 函式 infos=soup.find('ul',{'class':'list03'}).find_all('li') for info in infos: time=info.find('span').get_text() ajt= info.find('a').get_text() # 寫入檔案 f.write("{},{}\n".format(time,ajt))
結果
PS:如有需要Python學習資料的小夥伴可以加下方的群去找免費管理員領取
可以免費領取原始碼、專案實戰視訊、PDF檔案等