Created
December 28, 2017 04:47
-
-
Save ki111/49721897ce827eaa0b7dd7cc2acf047c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
import urllib2 | |
from bs4 import BeautifulSoup | |
import time | |
# 変数準備 | |
url = "https://www.ted.com/talks?page=" | |
total_page = 74 | |
count =0 | |
file = open('ted-link.csv','w') | |
file.write("#,link\n") | |
# スクレイビング開始 | |
for page_num in range(total_page): | |
# URLにアクセス、取得したページ内容をBeautifulsoupに格納し解析準備 | |
html = urllib2.urlopen(url+str(page_num)) | |
soup = BeautifulSoup(html, "html.parser") | |
# 進捗確認のため現ページ数を表示 | |
print(page_num) | |
# 過剰アクセスを防ぐため5秒休憩 | |
time.sleep(5) | |
# プレゼンへのリンクを取得 | |
for link in soup.find_all(class_=" ga-link"): | |
count = count + 1 | |
#1プレゼン当たり画像とタイトル計2個のリンクがあるため重複を防ぐ | |
if count % 2 == 0: | |
file.write(str(count/2)+","+link.get('href')+","+"\n") | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment