我在弄清楚我需要创建什么代码以使 python 在我的 csv 文件中尝试下一个 url 时遇到问题,每个 url 都在这样的一行上:
http : //www.indexedamerica.com/states/PR/Adjuntas/Restaurants-Adjuntas-00601.html http://www.indexedamerica.com/states/PR/Aguada/Restaurants-Aguada-00602.html http:// www.indexedamerica.com/states/PR/Aguadilla/Restaurants-Aguadilla-00603.html http: //www.indexedam erica.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)