我驾驶坚果试图从以下网址下载的CSV文件:蟒蛇下载文件:urllib2的ClientForm
有在网站4种形式,我设法设置正确的表格上的日期,然后我发布该表单,然后用正确的html获得http响应。但我想实际下载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)
注意,第一形式(日期选择器)是阵列的形式[1]中的形式。表单[2]是CSV文件或XML的选择框。选择CSV的代码是:
# form2 = forms2[2]
# # Select CSV file in selection control
# form2.find_control("menu_sari").items[1].selected = True # check
但我评论它,因为在回应后我没有得到这种形式。
任何帮助/反馈非常欢迎。
你看过使用这种机械化的东西吗? http://pypi.python.org/pypi/mechanize – 2012-01-12 12:39:08
我去了ClientForm网站,发现这个:“这个模块提供的功能现在是机械化的一部分,我不打算进一步独立版本的ClientForm”。尽管如此,我认为它可以用两种方法完成,但我做错了,因为它不检索CSV – user1145469 2012-01-12 14:15:35