Python - 从aspx表单下载文件

时间:2014-12-29 23:24:35

标签: python asp.net web-scraping urllib2

我试图从这个网站自动获取一些数据: http://www.casablanca-bourse.com/bourseweb/en/Negociation-History.aspx?Cat=24&IdLink=225

在python中使用urllib2,我成功地获得了一个html文件,好像我点击了"提交"这个网站上的按钮。

但是,当我模拟点击链接时的行为"下载数据"我得到了任何东西作为输出。

我的代码是:

import urllib
import urllib2

uri = 'http://www.casablanca-bourse.com/bourseweb/en/Negociation-History.aspx?Cat=24&IdLink=225'
headers = {
    'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)  Chrome/39.0.2171.95 Safari/537.36',
    'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
}

formFields = (
    (r'TopControl1$ScriptManager1', r'HistoriqueNegociation1$UpdatePanel1|HistoriqueNegociation1$HistValeur1$LinkButton1'),
    (r'__EVENTTARGET', r'HistoriqueNegociation1$HistValeur1$LinkButton1'),
    (r'__EVENTARGUMENT', r''),
    (r'__VIEWSTATE', r'/wEPDwUKMTcy/ ... +ZHYQBq1hB/BZ2BJyHdLM='), #just a small part because it's so long !
    (r'TopControl1$TxtRecherche', r''),
    (r'TopControl1$txtValeur', r''),
    (r'HistoriqueNegociation1$HistValeur1$DDValeur', r'9000  '),
    (r'HistoriqueNegociation1$HistValeur1$historique', r'RBSearchDate'),
    (r'HistoriqueNegociation1$HistValeur1$DateTimeControl1$TBCalendar', r'22/12/2014'),
    (r'HistoriqueNegociation1$HistValeur1$DateTimeControl2$TBCalendar', r'28/12/2014'),
    (r'HistoriqueNegociation1$HistValeur1$DDuree', r'6'),
    (r'hiddenInputToUpdateATBuffer_CommonToolkitScripts', r'1')
)


encodedFields = urllib.urlencode(formFields)

req = urllib2.Request(uri, encodedFields, headers)
f = urllib2.urlopen(req)

我应该怎样做以获得相同的文件,就像我点击"下载数据"链接在网站?

谢谢

1 个答案:

答案 0 :(得分:0)

首先,我建议您使用requests库而不是urllib。我们还需要一个BeautifulSoup来处理HTML标记:

pip install requests

pip install beautifulsoup4

比,代码看起来像这样:

import requests
from bs4 import BeautifulSoup

session = requests.Session()

payload = {
    r'TopControl1$ScriptManager1': r'HistoriqueNegociation1$UpdatePanel1|HistoriqueNegociation1$HistValeur1$LinkButton1',
    r'__EVENTTARGET': r'HistoriqueNegociation1$HistValeur1$LinkButton1',
    r'__EVENTARGUMENT': r'',
    r'TopControl1$TxtRecherche': r'',
    r'TopControl1$txtValeur': r'',
    r'HistoriqueNegociation1$HistValeur1$DDValeur': r'9000  ',
    r'HistoriqueNegociation1$HistValeur1$historique': r'RBSearchDate',
    r'HistoriqueNegociation1$HistValeur1$DateTimeControl1$TBCalendar': r'22/12/2014',
    r'HistoriqueNegociation1$HistValeur1$DateTimeControl2$TBCalendar': r'28/12/2014',
    r'HistoriqueNegociation1$HistValeur1$DDuree': r'6',
    r'hiddenInputToUpdateATBuffer_CommonToolkitScripts': r'1'
  }


uri = 'http://www.casablanca-bourse.com/bourseweb/en/Negociation-History.aspx?Cat=24&IdLink=225'
r = session.get(uri)

#Find __VIEWSTATE value, there is only one input tag with type="hidden"
soup = BeautifulSoup(r.text)
viewstate_tag = soup.find('input', attrs={"type" : "hidden"})
payload[viewstate_tag['name']] = viewstate_tag['value']

r = session.post(uri, payload)
print r.text #contains html table with data

首先,我们获取原始页面,提取__VIEWSTATE值并将该值用于第二个请求。