Python 2.7 我无法使用urllib、urllib2、mechanize库填写字段,我想提交表单
以下是我的urllib和urllib2代码示例:Python 2.7 我无法使用urllib、urllib2、mechanize库填写字段,我想提交表单,python-2.7,urllib2,mechanize,urllib,python-3.5,Python 2.7,Urllib2,Mechanize,Urllib,Python 3.5,以下是我的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 tim
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”:“请填写所有字段。”
另一个例子是mechanize:
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)
明白了:
回溯(最近一次呼叫最后一次):
文件“/Users/vasyaiv/Desktop/Automation test Python/draft.py”,第68行,在
br.选择表格(nr=0)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/mechanize/_mechanize.py”,
第524行,选择表格
raise FormNotFoundError(“无表单匹配”+说明)
FormNotFoundError:没有与nr 0匹配的表单
有什么想法吗?页面上没有表单,请尝试使用br.open(…)打开。 检查目标页面是否在html代码中包含表单 更多错误:
br['name']='Vasya'
br['Phone:']='1234567890'
等请发布这些错误所指示的相关PHP文件:
{“status”:false,“msg”:“请填写所有字段。”}
-哪些字段<代码>FormNotFoundError:没有与nr 0匹配的表单-哪个表单?另外,如果blablabla.com
只是一个占位符,请使用example.com
,因为我的工作代理不喜欢blablabla.com
。