简体   繁体   English

表示对象值__str__

[英]Representing object values __str__

I have the following code: 我有以下代码:

import urllib

class Get:

    def sendData(self):
        self.data = urllib.urlencode({"contains":"silabeador"})   
        self.u = urllib.urlopen('http://tip.iatext.ulpgc.es/silabas/default.aspx', data)

request=Get()
print (request.sendData)

My output or returned value of the data sent to url is the following: 我发送到url的数据的输出或返回值如下:

<bound method Get.sendData of <__main__.Get instance at 0x7f6089f5e050>>

How to can I get the value of the object Get and not their representation? 如何获取对象Get的值而不是它们的表示形式?

By the way, I am a newbie in python interested in send to some site a couple of values and that this site receive this values in a texbox and search and retrieve these search values. 顺便说一句,我是python的新手,有兴趣向一些站点发送几个值,并且该站点在texbox中接收此值并搜索和检索这些搜索值。 I say this, because request library http://docs.python-requests.org/en/latest/ it's a great alternative really? 我这样说是因为请求库http://docs.python-requests.org/en/latest/真的是一个很好的选择吗?

Best Regards 最好的祝福

EDIT 编辑

I have been organized my small code of this better way: 我已经用这种更好的方式组织了我的小代码:

import urllib

class Get:              
    def __init__(self):
        data = urllib.urlencode({"contains":"silabeador"})   
        u = urllib.urlopen('http://localhost:8000/login/', data)
        print(u.read())
request=Get()
print request

And when I try get the value of my small page (on my localhost machine) I get them, but at the end I see the object value too, the < main .Get instance at 0x7f052c4e00e0> value at the end ... 当我尝试获取小页面的值(在本地主机上)时,我得到了它们,但最后我也看到了对象值,最后的< main .Get实例位于0x7f052c4e00e0>值...

➜  ~  python2 pageGET.py          

<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="robots" content="NONE,NOARCHIVE">
  <title>403 Forbidden</title>
  <style type="text/css">
    html * { padding:0; margin:0; }
    body * { padding:10px 20px; }
    body * * { padding:0; }
    body { font:small sans-serif; background:#eee; }
    body>div { border-bottom:1px solid #ddd; }
    h1 { font-weight:normal; margin-bottom:.4em; }
    h1 span { font-size:60%; color:#666; font-weight:normal; }
    #info { background:#f6f6f6; }
    #info ul { margin: 0.5em 4em; }
    #info p, #summary p { padding-top:10px; }
    #summary { background: #ffc; }
    #explanation { background:#eee; border-bottom: 0px none; }
  </style>
</head>
<body>
<div id="summary">
  <h1>Prohibido <span>(403)</span></h1>
  <p>CSRF verificacion fallida. Solicitud abortada</p>


  <p>Estás viendo este mensaje porqué esta web requiere una cookie CSRF cuando se envían formularios. Esta cookie se necesita por razones de seguridad, para asegurar que tu navegador no ha sido comprometido por terceras partes.</p>
  <p>Si has inhabilitado las cookies en tu navegador, por favor habilítalas nuevamente al menos para este sitio, o para solicitudes del mismo origen.</p>

</div>

<div id="info">
  <h2>Help</h2>

    <p>Reason given for failure:</p>
    <pre>
    CSRF cookie not set.
    </pre>


  <p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when
  <a
  href="https://docs.djangoproject.com/en/1.8/ref/csrf/">Django's
  CSRF mechanism</a> has not been used correctly.  For POST forms, you need to
  ensure:</p>

  <ul>
    <li>Your browser is accepting cookies.</li>

    <li>The view function passes a <code>request</code> to the template's <a
    href="https://docs.djangoproject.com/en/dev/topics/templates/#django.template.backends.base.Template.render"><code>render</code></a>
    method.</li>

    <li>In the template, there is a <code>{% csrf_token
    %}</code> template tag inside each POST form that
    targets an internal URL.</li>

    <li>If you are not using <code>CsrfViewMiddleware</code>, then you must use
    <code>csrf_protect</code> on any views that use the <code>csrf_token</code>
    template tag, as well as those that accept the POST data.</li>

  </ul>

  <p>You're seeing the help section of this page because you have <code>DEBUG =
  True</code> in your Django settings file. Change that to <code>False</code>,
  and only the initial error message will be displayed.  </p>

  <p>You can customize this page using the CSRF_FAILURE_VIEW setting.</p>
</div>

</body>
</html>

**<__main__.Get instance at 0x7f052c4e00e0>**

by using 通过使用

print (request.sendData)

you are justing printing a method definition for sendData() . 您只是打印sendData()方法定义

In order to print the data, you just have to use print (request.data) since you defined it in your class method. 为了打印数据,您只需要使用print (request.data)因为您已经在类方法中定义了它。

EDIT: 编辑:

Let's take a look at what you are doing in your second __init__() method. 让我们看看您在第二个__init__()方法中正在做什么。

def __init__(self):
   1- data = urllib.urlencode({"contains":"silabeador"})   
   2- u = urllib.urlopen('http://localhost:8000/login/', data)
   3- print(u.read())

1- You are encoding data 1-您正在编码数据

2- You are opening an url 2-您正在打开一个网址

3- You are reading from the url and printing it. 3-您正在从URL中读取并打印。 The output is the html page. 输出是html页面。

Then next, on this line 接下来,在这条线上

request=Get()

You are creating an instance of Get and assigning it to request , Next, you are printing request , which is an object reference. 您将创建Get实例并将其分配给request ,接下来,您将打印request ,这是一个对象引用。

So you want to print out just the pages contents? 因此,您只想打印页面内容? You can either: 您可以:

1- Remove the print request 1-删除print request

2- Change your method to: 2-将您的方法更改为:

def __init__(self):
        data = urllib.urlencode({"contains":"silabeador"})   
        u = urllib.urlopen('http://localhost:8000/login/', data)
        self.data = u.read() #assign the page content to an attribute

request = Get()
print request.data #print the data attribute.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM