2016-09-16 2 views
0

MTA 정보 페이지에서 div를 잡으려고합니다. 내가 HTML을 가져 와서 BeautifulSoup로 구문 분석하면 일부 데이터가 누락 된 것 같습니다. beautifulsoup의 누락 ​​된 클래스

여기 난 그냥 모든 데이터를 얻고 있는지 확인하기 위해 인쇄를 사용하고 지금까지

from bs4 import BeautifulSoup 
import urllib # access the web 

# SUBWAY STATUS PROJECT 
userURL = "http://www.mta.info" # MTA SITE 

htmlfile = urllib.urlopen(userURL) #creates html file 
htmldoc = htmlfile.read() #creates html text 

soup = BeautifulSoup(htmldoc, 'html.parser')  

subChart = soup.find(id = 'subwayDiv') 

print subChart 

내 코드입니다. 내가 얻으려는 정보가 누락 된 것을 알 수 있습니다. 내가 직접 페이지를 보면 지하철 상태를 보여주는 수업과 함께 div가 누락 된 것을 볼 수 있습니다.

내가 프로그래밍에 아주 새로운 오전 그래서 나의 무지를 클래스 subwayCategory와 요소의 subchart 변수 찾는

+0

그들은 ajax에 의해 만들어지며 일반적인 정적 HTML이 아니기 때문에 다른 방법으로 시도하십시오. – kiviak

답변

0

마음과 속성 ID의 값을 보관하십시오. 예를 들어 : 클래스 subwayCategory와 사업부의 id 값이 123 지금과 용어 {ID}를 교체 http://www.mta.info/status/subway/{ID}

에 요청을 할 수있는 데이터

<div style="float: left; width: 220px; border-bottom: 1px solid #7B7B98; padding: 4px 0;"> 
<div class="span-11"><img alt="1 2 3 Subway" class="subwayIcon_123" src="http://www.mta.info/sites/all/modules/custom/servicestatus/images/img_trans.gif"/></div> 
<div class="subwayCategory" id="123" style="margin-top: 4px;"></div> 

의이 부분에서 원하는 이드

+0

그건 작동하지 않습니다. 브라우저 나 코드에서 사용해보십시오. –

0

데이터가 아약스 요청으로 검색되면 json 형식의 정보를 얻을 수 있습니다. 당신이 당신이 로 time.time (함께 얻을 수 타임 스탬프을 통과하는 데 필요한 유일한) 다음 바로 json 라이브러리를 구문 분석은 :

from time import time 
from json import load, loads 
import urllib 

url = "http://www.mta.info/service_status_json/{}".format(int(time())) 

json_dict = loads(load(urllib.urlopen(url))) 

from pprint import pprint as pp 
pp(json_dict) 

I는이 모든 출력을 추가하지 않습니다 너무 많이하지만 "BT" 우리가 얻을 사용하고 :

{u'line': [{u'Date': {}, 
      u'Time': {}, 
      u'name': u'Bronx-Whitestone', 
      u'status': u'GOOD SERVICE', 
      u'text': {}}, 
      {u'Date': {}, 
      u'Time': {}, 
      u'name': u'Cross Bay', 
      u'status': u'GOOD SERVICE', 
      u'text': {}}, 
      {u'Date': {}, 
      u'Time': {}, 
      u'name': u'Henry Hudson', 
      u'status': u'GOOD SERVICE', 
      u'text': {}}, 
      {u'Date': u'09/16/2016', 
      u'Time': u' 5:57AM', 
      u'name': u'Hugh L. Carey', 
      u'status': u'SERVICE CHANGE', 
      u'text': u"     <span class='TitleServiceChange' >Service Change</span>     <span class='DateStyle'>     &nbsp;Posted:&nbsp;09/16/2016&nbsp; 5:57AM     </span><br/><br/>     HLC - HOV Lane Open 6 AM to 10 AM. Two-Way Operations in effect. Three (3) lanes Manhattan-bound. One (1) lane Brooklyn-bound.    <br/><br/>    "}, 
      {u'Date': {}, 
      u'Time': {}, 
      u'name': u'Marine Parkway', 
      u'status': u'GOOD SERVICE', 
      u'text': {}}, 
      {u'Date': u'09/16/2016', 
      u'Time': u' 5:57AM', 
      u'name': u'Queens Midtown', 
      u'status': u'SERVICE CHANGE', 
      u'text': u"     <span class='TitleServiceChange' >Service Change</span>     <span class='DateStyle'>     &nbsp;Posted:&nbsp;09/16/2016&nbsp; 5:57AM     </span><br/><br/>     QMT - HOV Lane Open 6 AM to 10 AM. Two-Way Operation in effect. Three (3) lanes Manhattan bound. One (1) lane Queens bound.    <br/><br/>         <span class='TitlePlannedWork' >Planned Work</span>     <br/>     <P style='MARGIN: 0in 0in 0pt'><SPAN style=''Times New Roman';2016; Queens-Midtown Tunnel downtown exit; One lane closed. Use 37<SUP>th</SUP></FONT><FONT size=3> St tunnel exit for access to 2</FONT><SUP><FONT size=3>nd</FONT></SUP><FONT size=3> Ave. Motorists should allow extra time and may wish to use an alternate route if possible' Drivers should expect delays and plan accordingly. Motorists can sign up for MTA e-mail or text alerts at </FONT><SPAN style='COLOR: blue'><A href='http://www.mta.info/'><SPAN style='COLOR: #0563c1'><FONT size=3>www.mta.info</FONT></SPAN></A><FONT size=3> </FONT></SPAN><FONT size=3>and check the Bridges and Tunnels homepage or Facebook page for the latest information on this planned work.</FONT></FONT></SPAN></P>    <br/><br/>         <span class='TitlePlannedWork' >Planned Work</span>     <br/>     QMT- MANHATTAN PLAZA WORK REQUIRES CLOSURE OF 'CROSSTOWN' LANES FOR 2 MONTHS. CUSTOMERS SEEKING A CROSSTOWN MANHATTAN ROUTE USE THE UPTOWN LANES; EXPECT DELAYS.    <br/><br/>    "}, 
      {u'Date': u'08/15/2016', 
      u'Time': u' 3:56PM', 
      u'name': u'Robert F. Kennedy', 
      u'status': u'PLANNED WORK', 
      u'text': u"     <span class='TitlePlannedWork' >Planned Work</span>     <br/>     <P style='MARGIN: 0in 0in 0pt'><SPAN style='COLOR: #1f497d'><FONT size=3 face=Calibri>Starting Monday, August 15, 2016 and through early 2018, one lane will be closed on the Queens-to-Manhattan ramp at the Robert F. Kennedy Bridge for roadway rehabilitation. In addition, overnight on Thursday, August 18 and Friday, August 19, there will be a series of intermittent FULL ramp closures, lasting 15-20 minutes each.</FONT></SPAN></P>    <br/><br/>    "}, 
      {u'Date': {}, 
      u'Time': {}, 
      u'name': u'Throgs Neck', 
      u'status': u'GOOD SERVICE', 
      u'text': {}}, 
      {u'Date': u'09/16/2016', 
      u'Time': u' 5:28AM', 
      u'name': u'Verrazano-Narrows', 
      u'status': u'PLANNED WORK', 
      u'text': u"     <span class='TitlePlannedWork' >Planned Work</span>     <br/>     VNB: PLANNED WORK; S. I. BOUND LOWER LEVEL - ONE LANE CLOSED; EXPECT DELAYS.    <br/><br/>    "}]} 

그래서 그냥 DICT을 통과하고 당신이 원하는 무엇을 선택해야합니다.

+0

고마워 집에 올 때 나는 엷게하려고 노력할 것이다! –