2017-05-26 17 views
0

ここではウェブサイトはドロップダウンフィルタに基づいてデータを表示するので、静的なドロップダウン値を渡してデータを取得しようとしていますが、Pythonを使用してデータを取得

誰でもviewstateを使用しているasp.netウェブサイトのデータを取得する方法を知っていますか?

私は、ビューステートMACの検証は失敗しましたエラー

の下に取得しています。このアプリケーションがWebファームまたはクラスタによってホストされている場合は、<machineKey>構成で同じvalidationKeyおよび検証アルゴリズムが指定されていることを確認してください。 AutoGenerateはクラスタ内では使用できません。

のPythonスクリプト

import requests 
from bs4 import BeautifulSoup 

def get_viewstate(): 
url = "http://xlnindia.gov.in/frm_G_Cold_S_Query.aspx?ST=GJ" 
req = requests.get(url) 
data = req.text 

bs = BeautifulSoup(data) 
return bs.find("input", {"id": "__VIEWSTATE"}).attrs['value'] 

url = "http://xlnindia.gov.in/frm_G_Cold_S_Query.aspx?ST=GJ" 
data = {"__VIEWSTATE": get_viewstate(),"ST":'GJ', "ddldistrict":'AMR', "ddltaluka":'' ,"btnSearch":'Search'} 
req = requests.post(url, data) 

bs = BeautifulSoup(req.text) 
print(bs.prettify()) 
+1

セレンを試してみませんか? –

答えて

1

私はあなたがrequestsでこれを行うことができるとは思わないが、あなたは簡単にseleniumでこれを行うことができます。

セレンをインストールするには - pip install seleniumまたはpip3 install seleniumです。
Chromedriverをご使用のマシンのthis linkからダウンロードし、driverを作業ディレクトリにコピーしてください。

seleniumのドキュメントhereを読むことができます。

import time 
from selenium import webdriver 

url = "http://xlnindia.gov.in/frm_G_Cold_S_Query.aspx?ST=GJ" 
browser = webdriver.Chrome() 
browser.get(url) 

#change this if you want to change the state from Gujrat to something else 
#or you can change the state simply by changing the "?ST=GJ" part of the URL 
#state = browser.find_element_by_id("ddlState") 
#state.send_keys("BR") 

district = browser.find_element_by_id("ddldistrict") 
district.send_keys("AMR") 

#Skip this if you want to include all categories into the result 
category = browser.find_element_by_id("ddlCategory") 
category.send_keys("R") 

button = browser.find_element_by_id("btnSearch") 
button.click() 

time.sleep(10) 
browser.save_screenshot(browser.title + ".JPEG") 
html = browser.page_source 
print(html) 

browser.close() 
browser.quit() 

NOTE
あなたはseleniumとヘッドレスブラウザを使用したい場合は、PhantomJSを使用しています。 PhantomJSでこれを行う方法については、thisをお読みください。

+0

ありがとうございました。 Khairul Basar、それは完全に動作します –

+0

ちょっとMD。 Khairul Basarさんは、各フィールド値をmysqlデータベースに保存するのを手伝ってくれますか? –

+0

@JunedAnsari試してみることができます。 –

関連する問題