0
표준 출력이없고 스타일/ID 태그가없는 이전 웹 사이트를 크롤링하려는 경우 다음과 같이 표시됩니다. BeautifulSoup은 여러 표를 통해 값을 추출하기 위해 "제목"을 확인합니다.
<table BORDER="0" VALIGN="top" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
\t <tr>
\t \t <td ALIGN="left" VALIGN="top" WIDTH="175">
\t \t \t <strong>Surname</strong>
\t \t </td>
\t \t <td valign="top">
Bloggs
\t \t </td>
\t </tr>
\t <tr>
\t \t <td ALIGN="left" VALIGN="top" WIDTH="175">
\t \t \t <strong>Forename(s)</strong>
\t \t </td>
\t \t <td valign="top">
Joe
\t \t </td>
\t </tr>
\t <tr>
\t \t <td ALIGN="left" VALIGN="top" WIDTH="175">
\t \t \t <strong>Title</strong>
\t \t </td>
\t \t <td valign="top">
Mr
\t \t </td>
\t </tr>
\t <tr>
\t <td ALIGN="left" VALIGN="top" WIDTH="175">
\t \t \t <strong>Gender</strong>
\t \t </td>
\t \t <td valign="top">
Male
\t \t </td>
\t </tr>
\t <tr>
\t \t <td ALIGN="left" VALIGN="top" WIDTH="175">
\t \t \t <strong>Occupation</strong>
\t \t </td>
\t \t <td valign="top">
\t \t </td>
\t </tr>
\t <tr>
\t \t <td ALIGN="left" VALIGN="top" WIDTH="175">
\t \t \t <strong>Date of Birth</strong>
\t \t </td>
\t \t <td valign="top">
13/05/12
\t \t </td>
\t </tr>
</table>
파이썬 나의 접근 방식은 조금 긴 호흡 그러나 아이디어는 제목으로 왼쪽 TD를 확인하고 관련 데이터과 같이있는 권리 TD 잡아이었다
title, forename, surname, gender, occupation, dob = '', '', '', '', '', ''
tbl1 = soup.findAll('table')[1]
for tr in tbl1.findAll('tr'):
content = tr.findAll('td')
if content[0].text.strip() == 'Title':
title = content[1].text.strip()
if content[0].text.strip() == 'Forename(s)':
forename = content[1].text.strip()
if content[0].text.strip() == 'Surname':
surname = content[1].text.strip()
if content[0].text.strip() == 'Gender':
gender = content[1].text.strip()
if content[0].text.strip() == 'Occupation':
occupation = content[1].text.strip()
if content[0].text.strip() == 'Date of Birth':
dob = content[1].text.strip()
print('"' + title + '","' + forename + '","' + surname + '","' + gender + '","' + occupation + '","' + dob + '"')
내가 얻을 수 있지만 모든 테이블을 반복 할 때마다 : AttributeError : ResultSet 개체에 'findAll'특성이 없습니다. 당신은 아마도 단일 항목과 같은 항목 목록을 처리하고있을 것입니다. find()를 호출 할 때 find_all()을 호출 했습니까?