以下是我的代码示例,其中包含urllib和urllib2:
import urllib
import urllib2
url = 'http://example.com/schedule-appointment.php'
name = "Name:"
phone = "Phone:"
email = "E-mail:"
office = "Office:"
rq_date = "Requested date and time:"
alt_date = "Alternative date and time:"
comments = "Reason for visit:"
values = {
name : "Vasya",
phone : "1234567890",
email : "test@test.com",
office : "Madison Ave (NYC)",
rq_date : "01/29/2017 10:00 am" ,
alt_date : "01/29/2017 10:00 am",
comments : "this is a test"
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print(the_page)
并收到该消息:{“status”:false,“msg”:“请填写所有字段。”}
和机械化的另一个例子:
import mechanize
br = mechanize.Browser()
urlofmypage = 'http://www.example.com/schedule-appointment.php'
br.open(urlofmypage)
print(br.geturl())
br.select_form(nr=0)
br['Name:'] = ['Vasya']
br['Phone:'] = ['1234567890']
br['E-Mail:'] = ['test@test.com']
br['Office:'] = ['Madison Ave (NYC)']
br['Requested date and time:'] = ['01/29/2017 10:00 am']
br['Alternative date and time:'] = ['01/29/2017 10:00 am']
br['Reason for visit:'] = ['this is a test']
result = br.submit()
print(result)
得到了: http://www.example.com/schedule-appointment.php
追踪(最近一次通话): 文件“/ Users / vasyaiv / Desktop / Automation test Python / draft.py”,第68行,in br.select_form(NR = 0) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py”, 第524行,在select_form中 提出FormNotFoundError(“没有形式匹配”+描述) FormNotFoundError:没有匹配nr 0的表单
有什么想法吗?
答案 0 :(得分:0)
页面上没有表单,您尝试使用br.open(...)打开。 检查您的目标页面是否包含html代码中的表单。
更多错误:
您使用了错误的控件ID(“名称:”,“电话:”)。我怀疑,目标页面上的控件ID看起来像“名字”,“电话”等。
您尝试使用列表填充文本输入:
对:
br['name'] = 'Vasya'
br['Phone:'] = '1234567890'
等