0
CSV 파일로 구문 분석해야하는 HTML 표가 있습니다.HTML 표를 구문 분석하십시오.
import urllib2, datetime
olddate = datetime.datetime.strptime('5/01/13', "%m/%d/%y")
from BeautifulSoup import BeautifulSoup
print("dates,location,name,url")
def genqry(arga,argb,argc,argd):
return arga + "," + argb + "," + argc + "," + argd
part = 1
row = 1
contenturl = "http://www.robotevents.com/robot-competitions/vex-robotics-competition"
soup = BeautifulSoup(urllib2.urlopen(contenturl).read())
table = soup.find('table', attrs={'class': 'catalog-listing'})
rows = table.findAll('tr')
for tr in rows:
try:
if row != 1:
cols = tr.findAll('td')
for td in cols:
if part == 1:
keep = 0
dates = td.find(text=True)
part = 2
if part == 2:
location = td.find(text=True)
part = 2
if part == 3:
name = td.find(text=True)
for a in tr.findAll('a', href=True):
url = a['href']
# Compare Dates
if len(dates) < 6:
newdate = datetime.datetime.strptime(dates, "%m/%d/%y")
if newdate > olddate:
keep = 1
else:
keep = 0
else:
newdate = datetime.datetime.strptime(dates[:6], "%m/%d/%y")
if newdate > olddate:
keep = 1
else:
keep = 0
if keep == 1:
qry = genqry(dates, location, name, url)
print(qry)
row = row + 1
part = 1
else:
row = row + 1
except (RuntimeError, TypeError, NameError):
print("Error: " + name)
5/01/13 이후에 해당 테이블에서 모든 VEX 이벤트를 가져올 수 있어야합니다. 지금까지이 코드는 날짜에 대한 오류를 표시합니다. 수정이 불가능한 것 같습니다. 어쩌면 나보다 나은 사람이이 코드를 고칠 수 있습니까? 미리 감사드립니다, 스미스.
수정 # 1 : I는 무엇입니까하는 오류 :
Value Error: '\n10/5/13' does not match format '%m/%d/%y'
내가 먼저 문자열의 시작 부분에 줄 바꿈을 제거 할 필요가 있다고 생각합니다. EDIT # 2 : 출력없이 아무런 도움없이 실행하면됩니까?
당신은 그것을 위해 아름다운 수프를 사용할 필요가 없습니다
업데이트는 다음과 같이 코드를 추적, 나는
dates
의 가치를 얻을. python3 HTMLParser를 사용할 수 있습니다. https://github.com/schmijos/html-table-parser-python3/blob/master/html_table_parser/parser.py – schmijos