Python が csv ファイルの次の URL を試行するために作成する必要があるコードを理解するのに問題があります。各 URL は次のような行にあります。
http : //www.indexedamerica.com/states/PR/Adjuntas/Restaurants-Adjuntas-00601.html http://www.indexedam erica.com/states/PR/Aguada/Restaurants-Aguada-00602.html http:// www.indexedamerica.com/states/PR/Aguadilla/Restaurants-Aguadilla-00603.html http ://www.indexedamerica.com/states/PR/Aguadilla/Restaurants-Aguadilla-00604.html http://www.indexedamerica. com/states/PR/Aguadilla/Restaurants-Aguadilla-00605.html http ://www.indexedamerica.com/states/PR/Maricao/Restaurants-Maricao-00606.html http ://www.indexedam erica.com/states/ MI/Kent/Restaurants-Grand-Rapids-49503.html
#open csv file
#read csv file line by line
#Pass each line to beautiful soup to try
#If URL raises a 404 error continue to next line
#extract tables from url
from mechanize import Browser
from BeautifulSoup import BeautifulSoup
import csv
mech = Browser()
indexed = open('C://python27/longlist.csv')
reader = csv.reader(indexed)
html = mech.open(reader)
for line in html:
try:
mechanize.open(html)
table = soup.find("table", border=3)
else:
#!!!! try next url from file. How do I do this?
for row in table.findAll('tr')[2:]:
col = row.findAll('td')
BusinessName = col[0].string
Phone = col[1].string
Address = col[2].string
City = col[3].string
State = col[4].string
Zip = col[5].string
Restaurantinfo = (BusinessName, Phone, Address, City, State)
print "|".join(Restaurantinfo)