따라서이 웹 사이트에서 데이터를 수집하려면 Python 웹 스크래퍼를 작성해야합니다. https://online.portalberni.ca/WebApps/PIP/Pages/Search.aspx?templateName=permit%20reporting
보시다시피 날짜 필드에 수동으로 텍스트를 입력 할 수 없습니다. 이런 페이지에 대한 스크립트를 작성할 때 일반적으로 수행하는 작업입니다. 스크립트는 헤드리스 우분투 서버에서 매일 실행됩니다. 스크립트가 실행되는 날까지의 7 일 동안 날짜 범위를 선택할 수 있어야합니다. 다시 말하지만 일반적으로 텍스트를 입력하면 쉽게 할 수 있지만 여기서는 옵션이 아니라고 생각합니다. 이와 같은 자바 스크립트 요소로 이것을 수행하는 방법에 대한 아이디어가 있습니까?
이렇게하면 다음 페이지로 이동했습니다 (유사한 작업을 수행 할 수있는 다른 양식이 있음).
from requests import Session
from bs4 import BeautifulSoup as Bs
s = Session() # Keeps things stored in for future use
# If you look at the HTML, this is the "action" of the form (in this case happens to be the same as the form itself, not always true)
form_url = "https://online.portalberni.ca/WebApps/PIP/Pages/Search.aspx?templateName=permit%20reporting"
# Gets the HTML of the form
r = s.get(form_url)
html = Bs(r.text, "lxml")
form = html.find("form")
# Finds hidden inputs in the form that are necessary for a successful POST
hidden = form.find_all("input", {"type": "hidden"})
data = {i["name"]: i["value"] for i in hidden}
"""
There is javascript code that changes the form data before submission (onsubmit in the
form). I found this by using developer tools in chrome to see what the POST data actually
was, not by analyzing the javascript
"""
data["ctl00$FeaturedContent$ToolkitScriptManager1"] = "ctl00$FeaturedContent$updpnl_search|ctl00$FeaturedContent$btn_ViewReport"
data["__EVENTTARGET"] = ""
data["__EVENTARGUMENT"] = ""
data["__ASYNCPOST"] = "true"
data["ctl00$FeaturedContent$btn_ViewReport"] = "Search"
# Change to your date range
data["ctl00$FeaturedContent$txt_FromDate"] = "01/01/2021"
data["ctl00$FeaturedContent$txt_ToDate"] = "01/10/2021"
# Submits the form
headers = {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"Referer": "https://online.portalberni.ca/WebApps/PIP/Pages/Search.aspx?templateName=permit%20reporting",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36",
}
s.post(form_url, data=data, headers=headers)
# The page with the results you're looking for
results_url = "https://online.portalberni.ca/WebApps/PIP/Pages/PropBasedReportSelection.aspx?templateName=permit%20reporting"
r = s.get(results_url)
이 양식을 건너 뛰고 두 번째 페이지 양식 만 수행 할 수도 있지만 시도하지 않았습니다. 이것은 적어도 당신을 올바른 길로 인도해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다