2016-09-03 1 views
2

안녕 내가 파이썬에 새로 온 사람과 현재의 웹 사이트에 테이블에서 데이터를 다운로드하려고하지만 긁는 다 (http://www.pa.org.mt/AppList?ReceivedDate=2016-8-31)은 BeautifulSoup로는 - 테이블 데이터

나는 많은 다른 솔루션을 시도했지만 내가하려고 모든 것이 반환 유지 빈 목록. 그 문제는 테이블이 Javascript를 사용하여로드된다는 것일 수도 있지만 Javascript를 끄면 테이블이 남아 있고 소스 코드를 볼 때 원하는 데이터를 볼 수 있습니다. 나는이 코드를 실행하면 내가 파이썬 2.7

을 사용하고

: 테이블이 있어야 할 곳에

from bs4 import BeautifulSoup 
import urllib2 

url = 'http://www.pa.org.mt/AppList?ReceivedDate=2016-8-31 
page = urllib2.urlopen(url) 
soup = BeautifulSoup(page, 'html.parser') 
print soup 

내가 얻을 :

<link href="appsearch/main.css" rel="stylesheet" type="text/css" /> 
     <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="0"> 
      <TR> 
       <TD align=center> 

        <br /> 
        <br /> 
       </TD> 
      </TR> 
      <TR> 
       <TD> 
         </TD> 
      </TR> 
     </TABLE> 

내가 그러나 페이지의 소스 코드를 볼 때 I 내가 원하는 정보를 볼 수 있습니다. (복사하여 작은 부분을 붙여 넣었습니다.

<TD align=center> 
     <p align='center' class='H1'><u>Planning Authority  Applications Received (Planning Applications Outside Development Zone)</u></p><p align='center'>Result For Date 2016-8-31</p><p align='center'>Result output on 03/09/2016 23:23:29</p><strong><i>Disclaimer</strong>: The information ....in accordance with the Development Planning Act.</i> 
        <br /> 
        <br /> 
       </TD> 
      </TR> 
      <TR> 
       <TD> 
         <table class='formTable'><tr><td class='sectionHeading' colspan=2>Application Details</td></tr></table><table class='formTable'><TR><td class='sectionHeading'>Case Number</td><td class='sectionHeading'>Location</td><td class='sectionHeading'>Proposal</td><td class='sectionHeading'>Applicant</td><td class='sectionHeading'>Architect</td><td class='sectionHeading'>Case Category</td><td class='sectionHeading'>Local Council</td></tr><TR><td class='fieldData'><a href='SearchPA?Systemkey=166837&CaseFullRef=PA/05054/1 

나를 제안 할 수 있거나 나를 도와 줄 수있는 자료의 방향을 알려 주면 좋겠다.

이전에 말했듯이 파이썬과 stackoverflow에 익숙하지 않으므로 유사한 질문에 이미 답변했거나 올바른 정보를 제공하지 않은 경우 사과드립니다. 당신은 분명 캐시와 당신은 당신이 당신의 자신의 출력에 표시되는 모든처럼 데이터 볼 http://www.pa.org.mt/appsreceived?month=01/08/2016로 직접 이동하는 경우

감사

+0

아마도 javascript로 채워지므로 셀레늄 파이썬 패키지 –

답변

2

는 :

enter image description here

을 당신은을 사용할 필요가 첫 번째 페이지 앞에있는 페이지를 방문하십시오.

import requests 
head = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"} 

with requests.Session() as s: 
    s.headers.update(head) 
    s.get("http://www.pa.org.mt/appsreceived?CaseType=PA&Category=PAI") 

    r2 = (s.get("http://www.pa.org.mt/AppList?ReceivedDate=2016-8-31")) 
    print(r2.content) 

이제 다음 문제는, html로가 고장 html.parse 당신이 원하는 것을 얻을하지 않도록 : 또는 html5lib, 내가 출력을 추가하지 않습니다 것

In [4]: with requests.Session() as s: 
    ...:   s.headers.update(head) 
    ...:   r= s.get("http://www.pa.org.mt/appsreceived?CaseType=PA&Category=PAI") 
    ...:   page = (s.get("http://www.pa.org.mt/AppList?ReceivedDate=2016-8-31").content) 
    ...:   soup = BeautifulSoup(page, 'html.parser') 
    ...:   print(soup.select_one("#Table1")) 
    ...:  
<table border="0" cellpadding="1" cellspacing="1" id="Table1" width="100%"> 
<tr> 
<td align="center"> 
<p align="center" class="H1"><u>Planning Authority Applications Received (Planning Applications Within Development Zone)</u></p><p align="center">Result For Date 2016-8-31</p><p align="center">Result output on 04/09/2016 01:56:44</p><strong><i>Disclaimer</i></strong>: The information below has been extracted from an on-line database and is meant only for your general guidance.The Planning Authority disclaims any responsibility for any inaccuracies there may be on this site. If you wish to verify the correctness of any information then you are advised to contact us directly. Furtheremore, in the event of any discrepancies between the information contained on this site and official printed communication then the latter is to prevail, in accordance with the Development Planning Act.</td></tr></table> 

LXML을 꽤 크다으로하지만, 두 파서 중 하나를 사용하면 전체 테이블 데이터를 얻을 수 있습니다.

+0

을 확인하십시오. 완벽합니다. 이전 페이지를 방문한 적이 없을 것입니다. 배울 것이 많습니다. 이것 대단히 감사합니다. – YP41

+0

브라우저에서 페이지를 방문한 것처럼 확실한 문제는 아니지만 필요한 쿠키가 원래 프로세스를 거치지 않도록 직접 설정했을 때 작동합니다. –