【发布时间】:2014-04-09 18:05:33
【问题描述】:
stackoverflow 上已经有很多很好的资源,但我仍然遇到问题。我访问过这些来源:
- how to submit query to .aspx page in python
- Submitting a post request to an aspx page
- Scrapping aspx webpage with Python using BeautifulSoup
- http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet
我正在尝试访问http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx 并选择一个教区。我相信这会强制发布一个帖子,并允许我选择一年,再次发布,并允许更多选择。我已经按照上述来源以几种不同的方式编写了我的脚本,但未能成功提交网站以允许我输入年份。
我当前的代码
import urllib
from bs4 import BeautifulSoup
import mechanize
headers = [
('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Origin', 'http://www.indiapost.gov.in'),
('User-Agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'),
('Content-Type', 'application/x-www-form-urlencoded'),
('Referer', 'http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx'),
('Accept-Encoding', 'gzip,deflate,sdch'),
('Accept-Language', 'en-US,en;q=0.8'),
]
br = mechanize.Browser()
br.addheaders = headers
url = 'http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx'
response = br.open(url)
# first HTTP request without form data
soup = BeautifulSoup(response)
# parse and retrieve two vital form values
viewstate = soup.findAll("input", {"type": "hidden", "name": "__VIEWSTATE"})
eventvalidation = soup.findAll("input", {"type": "hidden", "name": "__EVENTVALIDATION"})
formData = (
('__EVENTVALIDATION', eventvalidation[0]['value']),
('__VIEWSTATE', viewstate[0]['value']),
('__VIEWSTATEENCRYPTED',''),
)
try:
fout = open('C:\\GIS\\tmp.htm', 'w')
except:
print('Could not open output file\n')
fout.writelines(response.readlines())
fout.close()
我也在 shell 中尝试过这个,我输入的内容和收到的内容(修改为减少体积)可以在 http://pastebin.com/KAW5VtXp 找到。
无论如何,我尝试更改教区下拉列表中的值并发布我被带到网站管理员登录页面。
我是否以正确的方式处理这个问题?任何想法都会非常有帮助。
谢谢!
【问题讨论】:
标签: python web-scraping urllib2 mechanize