[英]Not able to scrape data from dropdown box
在以下網站“ http://www.msamb.com/apmcpri_rpt.aspx ”中。
每次單擊下拉菜單中的元素時,輸出都會更改,但URL保持不變。 如果下拉菜單的值更改,它將調用Java腳本。 我跟蹤了網絡並檢查了請求標頭和表單鍵值,並在郵遞員中使用了它。 但是它每次都返回相同的頁面(“ http://www.msamb.com/apmcpri_rpt.aspx ”,下拉菜單中未選擇任何內容)。
有人可以幫忙抓取這個網站嗎?
每次您從下拉列表中選擇一個項目時,都會發送一個POST請求。 在您的代碼中模擬它。 requests
包將有助於維持您的網絡抓取會話。 樣例代碼:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.