使用内置的Python模块填充Web表单数据 [英] Filling Out Web Form Data Using Built-In Python Modules

查看:159
本文介绍了使用内置的Python模块填充Web表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我使用机械化,请求,美丽的汤,甚至是硒我的冒险做这样的事情,我已经得出结论,urllib和其他默认模块是最好的方式去。唯一的问题是我无法弄清楚如何使用它。所以有人可以告诉我一些很好的地方来了解这一点吗?另外我通过例子学习最好,所以如果有人会将它转换成我所要求的那样会很好(还包括一个提交按钮哈哈)

Alright so I have used mechanize, requests, beautiful soup, and even selenium on my venture to do something like this and I have come to the conclusion that urllib and the other default modules are the best way to go. Only problem is I can't figure out how to use it at all.. So can someone please show me some good places to learn about that specifically? Also I learn best by examples so if someone would convert this to what I am asking for that would be great (also include a submit button lol)

from selenium import webdriver

driver = webdriver.Firefox()

driver.get("http://www.jonessoda.com/contests/back2school")
element = driver.find_element_by_name("fname")
element.send_keys("Ben")


推荐答案

你确实需要Selenium。它模拟浏览器上的GUI交互。
在进行竞争形式数据等事情时,这将是最不可检测的方式。

You do want Selenium. It simulates GUI interactions on a browser. When doing things like entering competition form data, this is going to be the way that is least detectable.


硒:它不是一个语言特定的库。每种语言都有客户端特定的绑定。你会看到的大多数例子和方法实际上都是用Java编写的。

A note about selenium: It is not a language-specific library. There are client specific bindings for each language. Most examples and how-to's you'll see are actually written in Java.

一个好的资源是 Selenium-python

这是你的工作示例。包括提交按钮。

Here's your working example. Including submit button.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
i = 2 # do it 2 times
while i > 0:
    driver = webdriver.Firefox()
    driver.get("http://www.jonessoda.com/contests/back2school")

    def find_by_xpath(locator):
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, locator))
        )

        return element

    class FormPage(object):
        def fill_form(self, data):
            find_by_xpath('//input[@name = "fname"]').send_keys(data['fname'])
            find_by_xpath('//input[@name = "lname"]').send_keys(data['lname'])
            find_by_xpath('//input[@name = "email"]').send_keys(data['email'])
            find_by_xpath('//select[@name = "birthday_month"]').send_keys(data['month'])
            find_by_xpath('//select[@name = "birthday_day"]').send_keys(data['day'])
            find_by_xpath('//select[@name = "birthday_year"]').send_keys(data['year'])

            return self # makes it so you can call .submit() after calling this function

        def submit(self):
            find_by_xpath('//input[@value = "Submit"]').click()

    data = {
        'fname': 'Sheep',
        'lname': 'Test',
        'email': 'jess@sheeptest.com',
        'month': 'October',
        'day': '29',
        'year': '1920'
    }

    FormPage().fill_form(data).submit()
    driver.quit() # closes the webbrowser
    i = i - 1

这篇关于使用内置的Python模块填充Web表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆