Skip to content

Instantly share code, notes, and snippets.

@ishidur
Last active September 10, 2018 13:51
Show Gist options
  • Select an option

  • Save ishidur/7eb92e3d9eefe08517cc186ad630e515 to your computer and use it in GitHub Desktop.

Select an option

Save ishidur/7eb92e3d9eefe08517cc186ad630e515 to your computer and use it in GitHub Desktop.
Pythonで歴代総長をWebスクレイピング #code
import requests
from bs4 import BeautifulSoup
import csv
if __name__ == '__main__':
csvFile = open("ut_soutyou.csv", 'w', encoding='utf-8_sig', newline='')
writer = csv.writer(csvFile)
target_url = "http://www.u-tokyo.ac.jp/gen01/b01_03_j.html"
# target_url = "http://www.ynu.ac.jp/about/president/successive.html"
# Requestsを使って、webから取得
r = requests.get(target_url)
# 要素を抽出
soup = BeautifulSoup(r.content, 'lxml')
table = soup.findAll("table")[0]
elems = table.findAll("tr")
for e in elems:
csvRow = []
if len(e.findAll(['td', 'th'])) is 4:
csvRow.append(' ')
for cell in e.findAll(['td', 'th']):
print(cell.get_text())
csvRow.append(cell.get_text())
writer.writerow(csvRow)
csvFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment