This is the URL for the web site: https://dataviewer.pjm.com/dataviewer/pages/public/tieflows.jsf The data I want to scrape is in the pop up window after clicking “Tie Values”.
This is my Python script:
import requests
from bs4 import BeautifulSoup
url = 'https://dataviewer.pjm.com/dataviewer/pages/public/tieflows.jsf'
data = {"javax.faces.partial.ajax": "true",
"javax.faces.source": "chart1:j_idt293",
"javax.faces.partial.execute": "@all",
"javax.faces.partial.render": "chart1:valuesPanel",
"chart1:j_idt293": "chart1:j_idt293",
"chart1":"chart1",
"chart1:selectedTie": "",
"chart1:tieFlowState": "ACTUAL",
"chart1:chart1tieflowDataTable_scrollState": "0,0"}
head = {}
with requests.Session() as s:
init = s.get(url)
soup = BeautifulSoup(init.text, "lxml")
val = soup.find('input', {'id': 'javax.faces.ViewState'}).get('value')
print(val)
data["javax.faces.ViewState"] = val`enter code here`
r = s.post(url, data=data, headers=head)
print(r.text.strip())
I got the response:
<?xml version="1.0" encoding="UTF-8"?>
<partial-response>
<changes>
<update id="formLeftPanel:dialogMessages"><![CDATA[<div id="formLeftPanel:dialogMessages" class="ui-messages ui-widget" aria-live="polite"></div>]]></update>
<update id="globalMessages"><![CDATA[<div id="globalMessages" class="ui-messages ui-widget" aria-live="polite"></div>]]></update>
<update id="javax.faces.ViewState"><![CDATA[-459631281975233795:-1867808435873449001]]></update>
</changes>
</partial-response>
Which missed the one for the data part.
When I use the javascript code below in the browser console window, I can get the data part. Does anybody know why? Thx
My javascript code is:
const viewState = document.querySelector('#chart1 > input[name="javax.faces.ViewState"]').value
const formData = new FormData()
formData.set("javax.faces.partial.ajax", true)
formData.set("javax.faces.source", "chart1:j_idt293")
formData.set("javax.faces.partial.execute", "@all")
formData.set("javax.faces.partial.render", "chart1:valuesPanel")
formData.set("chart1:j_idt293", "chart1:j_idt293")
formData.set("chart1", "chart1")
formData.set("chart1:selectedTie", "")
formData.set("chart1:tieFlowState", "ACTUAL")
formData.set("chart1:chart1tieflowDataTable_scrollState", "0,0")
formData.set("javax.faces.ViewState", viewState)
const url = "https://dataviewer.pjm.com/dataviewer/pages/public/tieflows.jsf"
const response = await fetch(url, {
method: "POST",
headers: {
},
body: formData
})
console.log(await response.text())