python下载表单下的文件:urllib2 ClientForm

发布于 2024-12-26 06:36:04 字数 2255 浏览 4 评论 0原文

我正在疯狂地尝试从以下网址下载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城°AllureLove 2025-01-02 06:36:04

查看源代码,您可以看到,当您填写表单时,将调用 php 脚本来生成 csv 文件,结果是一个用于保存文件的对话框。

例如,在开启时:

http://www.opcom.ro/rapoarte/raportPIPsiVolumTranzactionat.php? lang=zh

如果您填写表格并选择 13/1/2012 和 csv,则会调用以下 url:

http://www.opcom.ro/rapoarte/export_csv_raportPIPsiVolumTranzactionat.php?zi=13&luna=1&an=2012&limba=ro

以下内容对我有用:

>>> import urllib2
>>> url = "http://www.opcom.ro/rapoarte/export_csv_raportPIPsiVolumTranzactionat.php?zi=13&luna=1&an=2012&limba=ro"
>>> request = urllib2.Request(url)
>>> response = urllib2.urlopen(request)
>>> print response.read()
"PIP si volum tranzactionat pentru ziua de livrare: 13/1/2012"

"","Pret mediu [lei/MWh]","Volum [MWh]"
"ROPEX_DAM_Base (1-24)","185.23","31226.488"
"ROPEX_DAM_Peak (7-22)","239.68","22773.036"
"ROPEX_DAM_Off_Peak (1-6) & (23-24)","76.32","8453.452"

"Interval","Pret de Inchidere a Pietei [lei/MWh]","Volum Tranzactionat [MWh]"
"1","29.99","876.148"
"2","70.00","1057.729"
"3","50.00","1058.868"
"4","50.00","1044.700"
"5","50.00","1061.574"
"6","61.71","1015.513"
"7","86.08","1070.586"
"8","181.00","1187.392"
"9","222.00","1434.829"
"10","230.00","1515.633"
"11","262.60","1539.495"
"12","226.00","1538.931"
"13","225.00","1559.273"
"14","271.00","1515.113"
"15","266.42","1513.220"
"16","250.00","1534.506"
"17","263.02","1481.099"
"18","298.00","1351.114"
"19","283.42","1336.266"
"20","280.00","1398.646"
"21","271.00","1450.183"
"22","219.34","1346.750"
"23","170.00","1219.772"
"24","128.88","1119.148"

>>> 

看看这是否有帮助。如果您发现表单无法正常工作,您可以随时按照其模式构建 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:

http://www.opcom.ro/rapoarte/raportPIPsiVolumTranzactionat.php?lang=en

If you fill out the forms choosing 13/1/2012 and csv the following url is called:

http://www.opcom.ro/rapoarte/export_csv_raportPIPsiVolumTranzactionat.php?zi=13&luna=1&an=2012&limba=ro

The following works for me:

>>> import urllib2
>>> url = "http://www.opcom.ro/rapoarte/export_csv_raportPIPsiVolumTranzactionat.php?zi=13&luna=1&an=2012&limba=ro"
>>> request = urllib2.Request(url)
>>> response = urllib2.urlopen(request)
>>> print response.read()
"PIP si volum tranzactionat pentru ziua de livrare: 13/1/2012"

"","Pret mediu [lei/MWh]","Volum [MWh]"
"ROPEX_DAM_Base (1-24)","185.23","31226.488"
"ROPEX_DAM_Peak (7-22)","239.68","22773.036"
"ROPEX_DAM_Off_Peak (1-6) & (23-24)","76.32","8453.452"

"Interval","Pret de Inchidere a Pietei [lei/MWh]","Volum Tranzactionat [MWh]"
"1","29.99","876.148"
"2","70.00","1057.729"
"3","50.00","1058.868"
"4","50.00","1044.700"
"5","50.00","1061.574"
"6","61.71","1015.513"
"7","86.08","1070.586"
"8","181.00","1187.392"
"9","222.00","1434.829"
"10","230.00","1515.633"
"11","262.60","1539.495"
"12","226.00","1538.931"
"13","225.00","1559.273"
"14","271.00","1515.113"
"15","266.42","1513.220"
"16","250.00","1534.506"
"17","263.02","1481.099"
"18","298.00","1351.114"
"19","283.42","1336.266"
"20","280.00","1398.646"
"21","271.00","1450.183"
"22","219.34","1346.750"
"23","170.00","1219.772"
"24","128.88","1119.148"

>>> 

See if this helps. If you find you can't get the forms working you can always build the url following their pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文