2017-12-15 2 views
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>
필드가 늘 심지어 빈 행을 표시 DB에 존재하지 않을 경우 문제는, 일부 추가 데이터는 단지의 두 가지 핵심 테이블 사이에 별도의 테이블로 추가됩니다됩니다 데이터가 나타나고 그 때 표시가 나타나지 않습니다.

파이썬 나의 접근 방식은 조금 긴 호흡 그러나 아이디어는 제목으로 왼쪽 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()을 호출 했습니까?

답변

0

당신은 헤더의 목록을 작성하고 사용할 수 있습니다 itertools.izip_longest :

import itertools 
import re 
headers = ['title', 'forename', 'surname', 'gender', 'occupation', 'dob'] 
from bs4 import BeautifulSoup as soup 
s = soup(web_data, 'lxml') 
new_s = [re.sub('\n+|\t+', '', i.text) for i in s.findAll('td')] 
final_data = {a:b for a, b in itertools.izip_longest(headers, [c for i, c in enumerate(new_s) if i%2 != 0])} 

출력 :

{'surname': u'Mr', 'title': u'Bloggs', 'dob': u'13/05/12', 'gender': u'Male', 'forename': u'Joe', 'occupation': u''} 
관련 문제