BASE_URL = "http://openapi.gbis.go.kr/ws/rest/buslocationservice?serviceKey=%2BFdkupBYoTx3q0Sd%2B6GFPa6NZ0Quorkb0guP7oMfTj8I75dQKX8vhMXO4QoY6KLZwx%2Bja8eT7irD11Gxv31t1g%3D%3D&routeId=200000085"
responses = requests.get(BASE_URL)
print (responses.status_code)
dom = BeautifulSoup(responses.content, "html.parser")
elements = dom.select("span.text")
element = elements[0]
If you run this code, the following index error occurs:
element = elements[0]
IndexError: list index out of range
I want to get routeId, stationId, stationSeq, etc. from that page No matter how many times I change the code, the index error is because I set the wrong tag name in select?
xml beautifulsoup parsing python3
If you connect to the url, it returns xml data, not html. Therefore, you should import data using xml parser, not html parser. If you look at the beautifulsoup document, use lxml parser as xml parser. First, install lxml parser and run the code below.
from bs4 import BeautifulSoup
import requests
BASE_URL = "http://openapi.gbis.go.kr/ws/rest/buslocationservice?serviceKey=%2BFdkupBYoTx3q0Sd%2B6GFPa6NZ0Quorkb0guP7oMfTj8I75dQKX8vhMXO4QoY6KLZwx%2Bja8eT7irD11Gxv31t1g%3D%3D&routeId=200000085"
responses = requests.get(BASE_URL)
soup = BeautifulSoup(responses.content, 'lxml-xml')
for busLocation in soup.findAll('busLocationList'):
stationId = busLocation.find('stationId')
plateNo = busLocation.find('plateNo')
print("StationId: " + stationId.string + " PlateNo: " + plateNo.string)
578 Understanding How to Configure Google API Key
581 PHP ssh2_scp_send fails to send files as intended
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.