2
我試圖以編程刮所有的紐約馬拉松賽的數據在這裏找到:http://web2.nyrrc.org/cgi-bin/htmlos.cgi/2112.6.038135231510781094到CSV。Webscraping與BeautifulSoup - 紐約馬拉松時報
兩個問題:
- 我不能想出一個辦法來遍歷所有不同年份,而無需手動提供對應於一個單一的一年,各年齡組0-99的URL。
只是專注於單一的URL(相當於一年的數據),我的代碼不會發現任何表。
def scrapeTime(url): f = urllib.urlopen(url) s = f.read() print " read: ", len(s), " bytes" f.close() soup = BeautifulSoup(s) print soup #iterate over all rows for row in table.findAll('tr'): col = row.findAll('td') FirstName=col[0].string # first column should have rank of runner LastName=col[1].string def main(): url2013 = "http://web2.nyrrc.org/cgi-bin/htmlos.cgi/2112.12.076919132010781094" scrapeTime(url2013)
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="http://web2.nyrrc.org/cgi-bin/htmlos.cgi/33272.1.016990806717006432">here</a>.</p> </body></html>
有時我得到的錯誤,'URLError:<錯誤的urlopen [錯誤10060]連接嘗試失敗,因爲連接的方沒有正確一段時間後響應或已建立的連接失敗,因爲連接的主機沒有respond.'任何想法會導致什麼? – Parseltongue 2014-09-19 05:10:51
@Parseltongue可能這是因爲撞擊的部位過於頻繁的。嘗試在請求之間添加延遲('time.sleep()')。 – alecxe 2014-09-19 05:40:27