加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如果name = hash则自动填充

发布时间:2020-12-20 13:32:04 所属栏目:Python 来源:网络整理
导读:我试图在 python中使用mechanize自动填充表单中的文本框(多个框),但是框的名称是一个哈希,所以我不能像br.form [‘name’]那样自动输入输入=’blah’,因为该名称是来自散列函数的未知散列.有没有办法做到这一点?我在网上看过,但一直找不到任何东西.谢谢!
我试图在 python中使用mechanize自动填充表单中的文本框(多个框),但是框的名称是一个哈希,所以我不能像br.form [‘name’]那样自动输入输入=’blah’,因为该名称是来自散列函数的未知散列.有没有办法做到这一点?我在网上看过,但一直找不到任何东西.谢谢!

解决方法

这应该适合你.显然,您需要更新谓词方法.另外,您有关于该领域的任何持续信息吗? id,class,label等?

import mechanize
import re

class MyBrowser:

    def __init__(self):
        self.user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)'
        self.cj = mechanize.LWPCookieJar()
        self.setup_browser(enable_debug=False)

    def find_by_crazy_name(self,control):
        if re.match('^[w]{32,}$',control.name):
            return True
        return False

    def runit(self):
        self.agent.open('http://localhost')
        self.agent.select_form(name="foo")
        field = self.agent.form.find_control(predicate=self.find_by_crazy_name)
        field._value = "POOP"
        response = self.agent.submit()

    def enable_debug(self):
        self.agent.set_debug_http(True)
        self.agent.set_debug_redirects(True)
        self.agent.set_debug_responses(True)

    def setup_browser(self,enable_debug=False):
        self.agent = mechanize.Browser()
        self.agent.set_handle_redirect(True)
        self.agent.set_cookiejar(self.cj)
        self.agent.set_handle_referer(True)
        self.agent.set_handle_refresh(True)
        self.agent.set_handle_equiv(True)
        self.agent.set_handle_robots(False)
        self.enable_debug()
        self.agent.addheaders = [('User-Agent',self.user_agent)]


if __name__ == "__main__":
    browser = MyBrowser()
    browser.runit()

这只是填补了“POOP”的所有潜在领域.如果是32个字母数字字符(如md5),则字段匹配

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读