Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 我无法使用urllib、urllib2、mechanize库填写字段,我想提交表单_Python 2.7_Urllib2_Mechanize_Urllib_Python 3.5 - Fatal编程技术网

Python 2.7 我无法使用urllib、urllib2、mechanize库填写字段,我想提交表单

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

以下是我的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”:“请填写所有字段。”

另一个例子是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代码中包含表单

更多错误:

  • 您使用了错误的控件ID(“名称:”、“电话:”)。我怀疑,目标页面上控件的ID看起来像“name”、“phone”等

  • 您尝试使用列表填充文本输入: 正确的:
    br['name']='Vasya'
    br['Phone:']='1234567890'


  • 请发布这些错误所指示的相关PHP文件:
    {“status”:false,“msg”:“请填写所有字段。”}
    -哪些字段<代码>FormNotFoundError:没有与nr 0匹配的表单-哪个表单?另外,如果
    blablabla.com
    只是一个占位符,请使用
    example.com
    ,因为我的工作代理不喜欢
    blablabla.com