【问题标题】:I cannot fill out field using urllib, urllib2, mechanize libraries, I want to submit a form我无法使用 urllib、urllib2、机械化库填写字段,我想提交表单
【发布时间】:2017-05-28 07:47:56
【问题描述】:

这是我的带有 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

Traceback(最近一次调用最后一次): 文件“/Users/vasyaiv/Desktop/Automation test Python/draft.py”,第 68 行,在 br.select_form(nr=0) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py”, 第 524 行,在 select_form 中 raise FormNotFoundError("没有匹配的表单"+description) FormNotFoundError: 没有匹配 nr 0 的表单

有什么想法吗?

【问题讨论】:

  • 请按照这些错误的指示发布相关的 PHP 文件:{"status":false,"msg":"Please fill out all the fields."} - 哪些字段? FormNotFoundError: no form matching nr 0 - 哪种形式?另外,如果blabla.com 只是一个占位符,请使用example.com,因为我的工作代理不喜欢blabla.com

标签: python-2.7 urllib2 mechanize urllib python-3.5


【解决方案1】:

页面上没有表单,您尝试使用 br.open(...) 打开。 检查您的目标页面是否包含 html 代码中的表单。

更多错误:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多