在以下网站" http://www.msamb.com/apmcpri_rpt.aspx"。
每次单击下拉列表中的元素时输出都会更改,但url保持不变。如果下拉列表的值发生变化,它会调用java脚本。我跟踪网络并检查了请求标头并形成键值并在邮递员中使用它。但它每次都返回相同的页面(" http://www.msamb.com/apmcpri_rpt.aspx"在下拉菜单中没有选择任何内容)。
有人可以帮助抓取这个网站吗?
答案 0 :(得分:0)
每次从下拉列表中选择项目时都会发送POST请求。在代码中模拟它。 requests
package有助于维护您的网络抓取会话。示例代码:
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