我已经设法达到了这样一个地步:我曾经用BeautifulSoup从url中提取一个表。现在我想将输出格式化为一个表,以便在GeekTool中使用它。在from bs4 import BeautifulSoup
import urllib2
wiki = "https://www.google.com/maps/place?q=type:transit_station:%22145+St%22&ftid=0x89c2f67c67a250f9:0x92d51daa07480dd1"
header = {'User-Agent': 'Mozilla/5.0'} #Needed to prevent 403 error on Wikipedia
req = urllib2.Request(wiki,headers=header)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
desination = ""
eta = ""
table = soup.find("table", { "class" : "pprtjt" })
for row in table.findAll("tr"):
for cell in row.findAll("td"):
print cell.findAll(text=True)
其输出如下:
^{pr2}$
因此,第一行是第一列中的第一行,第二行是第to列中的第一行,依此类推,例如:C to 168 St | 2 min
D to Norwood - 205 St | 4 min
A to Ozone Park - Lefferts Blvd | 4 min
A to Inwood - 207 St | 5 min
D to Coney Island - Stillwell Av | 10 min
C to 168 St | 15 min
D to Norwood - 205 St | 19 min
A to Far Rockaway - Mott Av | 19 min
A to Inwood - 207 St | 20 min
我在这方面完全是个业余爱好者,所以如果这是一个完全错误的方法,请道歉。提前谢谢。在