# 實例操作-1 爬取系網教授gmail ### 1.先檢查robots.txt ```python= import requests from bs4 import BeautifulSoup url = "https://www.che.nthu.edu.tw/robots.txt" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") print(soup) ``` > output: >See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file >To ban all spiders from the entire site uncomment the next >two lines: >User-agent: * >Disallow: / ### 2.開始爬取資料 ```python= import requests from bs4 import BeautifulSoup f = open("Professors_Gmail", "w") #系網教授簡介共有3頁 for page in range(1,4): #觀察網址變化跟page有關,將其代碼取下 response = requests.get("https://www.che.nthu.edu.tw/zh_tw/Members/Appointed?page_no=" + str(page) + "&") #轉成HTML形式 soup = BeautifulSoup(response.text, "html.parser") #選取特定資料 sel_names = soup.select("span.i-member-value member-data-value-name a") sel_gmails = soup.select("span.i-member-value member-data-value-email a") #寫入檔案 for i in range(len(sel_gmails)): f.write(sel_names[i].text + ":" + sel_gmails[i].text + '\n') f.close() ``` >Professors_Gmail: > >蔡德豪 De-Hao Tsai: dhtsai@mx.nthu.edu.tw 黃振煌 Jen-Huang (Tony) Huang: jenhuang@mx.nthu.edu.tw 宋信文 Hsing-Wen Sung: hwsung@mx.nthu.edu.tw 汪上曉 David Shan-Hill Wong: dshwong@che.nthu.edu.tw 呂世源 Shih-Yuan Lu: sylu@mx.nthu.edu.tw 胡育誠 Yu-Chen Hu: ychu@mx.nthu.edu.tw 何榮銘 Rong-Ming Ho: rmho@mx.nthu.edu.tw 胡啟章 Chi-Chang Hu: cchu@che.nthu.edu.tw 陳信文 Sinn-Wen Chen: swchen@mx.nthu.edu.tw 陳信龍 Hsin-Lung Chen: hslchen@mx.nthu.edu.tw 陳壽安 Show-An Chen: sachen@che.nthu.edu.tw 劉英麟 Ying-Ling Liu: liuyl@mx.nthu.edu.tw 鄭西顯 Shi-Shang Jang: ssjang@mx.nthu.edu.tw 段興宇 Hsing-Yu Tuan: hytuan@che.nthu.edu.tw 堀江正樹 Masaki Horie: mhorie@mx.nthu.edu.tw 姚遠 Yuan Yao: yyao@mx.nthu.edu.tw 衛子健 Tzu-Chien Wei: tcwei@mx.nthu.edu.tw 王潔 Jane Wang: janewang@mx.nthu.edu.tw 沈若樸 Claire Roa-Pu Shen: crshen@mx.nthu.edu.tw 區迪頤John Ou: jdyou123@hotmail.com 周鶴修 Ho-Hsiu Chou: hhchou@mx.nthu.edu.tw 潘詠庭 Yung-Tin (Frank) Pan: ytpan@mx.nthu.edu.tw 楊東翰Tung-Han Yang: tunghanyang@mx.nthu.edu.tw 林育正Yu-Jeng Lin: yujeng.lin@mx.nthu.edu.tw 林昆翰 Kun-Han Lin: kunhan.lin@mx.nthu.edu.tw
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up