python下载表单下的文件:urllib2 ClientForm
我正在疯狂地尝试从以下网址下载 csv 文件:
网站中有 4 个表单,我设法在正确的表单上设置日期,然后我发布该表单,并得到正确的 http 响应html。但我想实际下载 csv 而不是响应的 html。我认为我必须提交 2 个表格,第一个是日期,然后是 csv 选择,但在第一个响应中我没有得到任何可以对话的表格。
这是我的代码:
#!/usr/bin/env python
import csv
from urllib2 import urlopen
from ClientForm import ParseResponse
import urllib2
proxy = urllib2.ProxyHandler({'http': '172.26.10.100:8080'})
# proxy = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/2010010' \
'1 Firefox/4.0.1',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'en-us,en;q=0.5',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7'}
# set the request
url = "http://www.opcom.ro/rapoarte/raportPIPsiVolumTranzactionat.php?lang=en"
request = urllib2.Request(url, None, headers)
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, response:
pass
print response.geturl()
print response.info() # headers
# print response.read() # body
# get forms from response
forms = ParseResponse(response, backwards_compat=False)
response.close()
# print "###FORMS: " ,len(forms)
# for i in range(len(forms)):
# print "@@@@@"
# print forms[i]
form1 = forms[1]
# setting a specific date in the form
form1.set_value("7", kind="text", nr=0)
form1.set_value("10", kind="text", nr=2)
form1.set_value("2011", kind="text", nr=4)
print form1
# # # SEND THE FORM
request2 = forms[1].click() # urllib2.Request object BIEN
try:
response2 = urllib2.urlopen(request2)
except urllib2.HTTPError, response2:
pass
print response2.geturl()
print response2.info() # headers
# print response2.read() # body
with open('salida.txt', 'w') as f:
f.write(response2.read())
forms2 = ParseResponse(response2, backwards_compat=False)
response2.close()
print "###FORMS 2: " ,len(forms2)
请注意,第一个表单(日期选择器)是 forms 数组中的 forms[1]。 forms[2]是CSV文件或XML文件的选择框。选择 CSV 的代码是:
# form2 = forms2[2]
# # Select CSV file in selection control
# form2.find_control("menu_sari").items[1].selected = True # check
但我评论了它,因为在响应之后我没有收到此表单。
非常欢迎任何帮助/反馈。
i'm driving nuts trying to download csv files from the following url:
There are 4 forms in the website, and I managed to set the date on the right form, then I post that form, and I get the http response with the right html. But i want to actually download the csv not the html of the response. I thought that i would have to submit 2 forms, first the date, and after the csv selection, but in the 1st response i don't get any form to dialogue with.
here is my code:
#!/usr/bin/env python
import csv
from urllib2 import urlopen
from ClientForm import ParseResponse
import urllib2
proxy = urllib2.ProxyHandler({'http': '172.26.10.100:8080'})
# proxy = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/2010010' \
'1 Firefox/4.0.1',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'en-us,en;q=0.5',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7'}
# set the request
url = "http://www.opcom.ro/rapoarte/raportPIPsiVolumTranzactionat.php?lang=en"
request = urllib2.Request(url, None, headers)
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, response:
pass
print response.geturl()
print response.info() # headers
# print response.read() # body
# get forms from response
forms = ParseResponse(response, backwards_compat=False)
response.close()
# print "###FORMS: " ,len(forms)
# for i in range(len(forms)):
# print "@@@@@"
# print forms[i]
form1 = forms[1]
# setting a specific date in the form
form1.set_value("7", kind="text", nr=0)
form1.set_value("10", kind="text", nr=2)
form1.set_value("2011", kind="text", nr=4)
print form1
# # # SEND THE FORM
request2 = forms[1].click() # urllib2.Request object BIEN
try:
response2 = urllib2.urlopen(request2)
except urllib2.HTTPError, response2:
pass
print response2.geturl()
print response2.info() # headers
# print response2.read() # body
with open('salida.txt', 'w') as f:
f.write(response2.read())
forms2 = ParseResponse(response2, backwards_compat=False)
response2.close()
print "###FORMS 2: " ,len(forms2)
Notice that the first form (date picker) is forms[1] in the forms array. And the forms[2] is the selection box for CSV file or XML. The code to select CSV is:
# form2 = forms2[2]
# # Select CSV file in selection control
# form2.find_control("menu_sari").items[1].selected = True # check
But i commented it since after the response i don't get this form.
Any help/feedback it's very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看源代码,您可以看到,当您填写表单时,将调用 php 脚本来生成 csv 文件,结果是一个用于保存文件的对话框。
例如,在开启时:
如果您填写表格并选择 13/1/2012 和 csv,则会调用以下 url:
以下内容对我有用:
看看这是否有帮助。如果您发现表单无法正常工作,您可以随时按照其模式构建 url。
In looking at the source you can see that when you fill out the form a php script is called to generate the csv file and the result is a dialog box to save the file.
For instance while on:
If you fill out the forms choosing 13/1/2012 and csv the following url is called:
The following works for me:
See if this helps. If you find you can't get the forms working you can always build the url following their pattern.