웹 사이트에서 표 형식의 콘텐츠를 구문 분석하기 위해 Python으로 스크립트를 만들었습니다. 이제 내 스크립트가 랜딩 페이지의 콘텐츠를 구문 분석 할 수 있습니다. 그러나 NEXT PAGE
그 페이지 하단에 클릭하면 50 개 이상의 결과를 보여주는 버튼이 있습니다.
나는 시도했다 (처음 50 개의 결과를 긁어 냄) :
import requests
from bs4 import BeautifulSoup
site_link = 'https://indiarailinfo.com/trains/passenger/0/0/0/0'
res = requests.get(site_link)
soup = BeautifulSoup(res.text,"lxml")
for items in soup.select("div[style='line-height:20px;']"):
tds = [elem.get_text(strip=True) for elem in items.select("div")]
print(tds)
요청을 사용하여 다음 페이지 버튼을 소모하는 해당 페이지에서 모든 표 형식의 콘텐츠를 가져 오려면 어떻게해야합니까?
추신 셀레늄을 사용하여 콘텐츠를 펼치는 방법을 알고 있으므로 브라우저 시뮬레이터와 관련된 솔루션은 내가 원하는 것이 아닙니다.
다음 버튼을 클릭하면 실제로 https://indiarailinfo.com/trains/passenger/0/1?i=1&&kkk=1571329558457에 XHR을 수행합니다.
<button class="nextbtn" onclick="javascript:getNextTrainListPageBare($(this).parent(),'/trains/passenger/0/1?i=1&');"><div>NEXT PAGE<br>the next 50 Trains will appear below</div></button>
따라서 'onclick'에서 데이터를 가져오고 URL을 작성하고 요청을 사용하여 HTTP GET을 수행하면됩니다.
반환 된 데이터는 다음과 같습니다.
이제 BeautifulSoup을 사용하고 필요한 데이터를 추출하십시오.
아래 코드 (10을 필요한 번호로 대체)
import requests
from bs4 import BeautifulSoup
site_link = 'https://indiarailinfo.com/trains/passenger/0/{}'
for x in range(10):
url = site_link.format(x)
res = requests.get(url)
soup = BeautifulSoup(res.text,"lxml")
print('Data for url: {}'.format(url))
for items in soup.select("div[style='line-height:20px;']"):
tds = [elem.get_text(strip=True) for elem in items.select("div")]
print(tds)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다