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