In the following website " http://www.msamb.com/apmcpri_rpt.aspx ".
The output change every time I click on an element in a dropdown but the url remains same. It is calling a java script if the value of the drop down changes. I tracked the Network and checked the request headers and form key-values and used it in postman. But it is returning the same page every time(" http://www.msamb.com/apmcpri_rpt.aspx " with nothing selected in dropdown).
Can someone please help in scraping this site?
There is a POST request sent each time you select an item from the dropdown. Simulate it in your code. requests
package would help maintaining your web-scraping session. Sample code:
from bs4 import BeautifulSoup
import requests
apmc = 'JALGAON'
url = 'http://www.msamb.com/apmcpri_rpt.aspx'
with requests.Session() as session:
session.headers = {
'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
'X-Requested-With': 'XMLHttpRequest'
}
response = session.get(url)
soup = BeautifulSoup(response.content)
# build an options mapping
options = {option.get_text(strip=True): option['value'] for option in soup.select("select#cpMainContent_cmb_comm option")[1:]}
# parse form parameters
form = soup.find("form", id="form1")
params = {
'ctl00$cpMainContent$cmb_comm': options.get(apmc),
'__ASYNCPOST': 'true',
'ctl00$cpMainContent$ScriptManager1': 'ctl00$cpMainContent$UpdatePanel1|ctl00$cpMainContent$cmb_comm',
'__EVENTTARGET': 'ctl00$cpMainContent$cmb_comm',
'__EVENTARGUMENT': form.find('input', {'name': '__EVENTARGUMENT'})['value'],
'__LASTFOCUS': '',
'__VIEWSTATE': form.find('input', {'name': '__VIEWSTATE'})['value'],
'__VIEWSTATEGENERATOR': form.find('input', {'name': '__VIEWSTATEGENERATOR'})['value'],
'__VIEWSTATEENCRYPTED': '',
'__EVENTVALIDATION': form.find('input', {'name': '__EVENTVALIDATION'})['value']
}
response = session.post(url, data=params)
# parse the results
soup = BeautifulSoup(response.content)
for row in soup.select("table#cpMainContent_GridView1_tab5 tr")[1:]:
print row.find_all("td")[1].text
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.