问题描述
所以我需要抓取这个网站使用python,但是我在尝试提交表单时发现了一个问题.我得到的回复是与表单相同的页面,而不是提交表单后的结果.我尝试使用请求 library/mechanize/urllib
.
So I need to crawl this website using python, however I am finding a problem when trying to submit the form. The response I get is the same page with the form and not the result after submitting the form. I tried to use requests library/ mechanize / urllib
.
带有请求的代码:
url = "http://www.justiceservices.gov.mt/courtservices/Judgements/search.aspx?func=selected"
payload = {'ctl00$ContentPlaceHolderMain$search_selected_panel$tb_date_from':'',
'ctl00$ContentPlaceHolderMain$search_selected_panel$tb_date_to':'',
'ctl00$ContentPlaceHolderMain$search_selected_panel$dd_court':108,
'ctl00$ContentPlaceHolderMain$search_selected_panel$dd_judiciary':'',
'ctl00$ContentPlaceHolderMain$search_selected_panel$tb_litigant1':'',
'ctl00$ContentPlaceHolderMain$search_selected_panel$tb_litigant2':'',
'ctl00$ContentPlaceHolderMain$search_selected_panel$tb_keywords':'',
'ctl00$ContentPlaceHolderMain$search_selected_panel$keywords':'rb_keywords_matching_all',
'ctl00$ContentPlaceHolderMain$search_selected_panel$bt_search':'Search',
'ctl00$ContentPlaceHolderMain$search_selected_panel$result_count_panel$dd_result_count':10}
headers = {'content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url,payload,allow_redirects=True)
print r.headers
print r.text
我需要发布额外的数据吗?或者我的方法对表格类型是错误的.该网站使用网络表单.
Do I need to post additional data? or my approach is wrong to the type of form. The website uses web-forms.
推荐答案
如果你查看请求源,特别是 https://github.com/kennethreitz/requests/blob/master/requests/api.py#L80 您会看到该帖子忽略了参数.如果没有时间进行测试,您似乎需要这样做:
If you look at the requests source, specifically https://github.com/kennethreitz/requests/blob/master/requests/api.py#L80 you'll see that post ignores args. Without having time to test, it would seem likely you need to do:
r = requests.post(url, data=payload, allow_redirects=True
这篇关于Python 使用请求提交网络表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!