小编Tyl*_*ler的帖子

Python错误:输入错误:POST数据应为字节; 还有用户代理问题

使用以下代码我收到一个错误:

TypeError: POST data should be bytes or an iterable of bytes. It cannot be str
Run Code Online (Sandbox Code Playgroud)

第二个问题,我不确定我是否正确指定了我的用户代理,这里是我的用户代理:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4.当我在脚本中定义用户代理时,我给了我最好的镜头.

import urllib.parse
import urllib.request

url = 'http://getliberty.org/contact-us/'
user_agent = 'Mozilla/5.0 (compatible; Chrome/22.0.1229.94; Windows NT)'
values = {'Your Name' : 'Horatio',
          'Your Email' : '6765Minus4181@gmail.com',
          'Subject' : 'Hello',
          'Your Message' : 'Cheers'}

headers = {'User-Agent': user_agent }

data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req)
the_page = response.read()
Run Code Online (Sandbox Code Playgroud)

我知道这个类似的问题,TypeError:POST数据应该是字节或可迭代的字节.它不可能是str,但是对于答案来说太新了太多帮助.

post python-3.x

10
推荐指数
1
解决办法
1万
查看次数

如何设置“阅读文档”以使 Sphinx autodoc 选项起作用?

我的项目不是使用 autodoc 构建的。我遇到了有关我的项目未在 autodoc 中构建的常见问题。但是,某些依赖项包含不会在构建文档服务器上执行的 C 代码。因此,我阅读了此博客中的方法,解释我应该使用模拟。这与 stackoverflow 问题“how-to-mock-so-that-from-x-import-works”相关。

在阅读文档的管理页面的高级设置部分中,有一个使用 virtualenv 的选项,我检查了该选项,然后请求了我的项目根目录到requirements.txt的路径。

项目目录结构如下:

GatherNews/
    requirements.txt
Run Code Online (Sandbox Code Playgroud)

当我用作GatherNews/requirements.txt路径时。我收到错误:

/var/build/user_builds/gathernews/checkouts/latest/docs/api/grss.rst:10:警告:autodoc:无法从模块 u'gathernews.gRSS' 导入类 u'CaptureFeeds';提出了以下例外情况:

回溯(最近一次调用最后一次):

文件“/home/docs/checkouts/readthedocs.org/user_builds/gathernews/envs/latest/local/lib/python2.7/site-packages/sphinx/ext/autodoc.py”,第 335 行,在 import_object import (self .modname)

文件“/home/docs/checkouts/readthedocs.org/user_builds/gathernews/envs/latest/local/lib/python2.7/site-packages/gathernews/ init .py”,第 1 行,导入 gRSS

文件“/home/docs/checkouts/readthedocs.org/user_builds/gathernews/envs/latest/local/lib/python2.7/site-packages/gathernews/gRSS.py”,第 38 行,导入 feedparser

ImportError:没有名为 feedparser /var/build/user_builds/gathernews/checkouts/latest/docs/_themes/README.rst:: 警告:文档不包含在任何目录树中

我的问题是如何将这一切联系在一起?具体来说,我是否使用正确的路径来requirements.txt阅读文档以成功构建?如果我的路径requirements.txt正确,那么如何包含模拟包以成功生成自动文档?

python git documentation mocking documentation-generation

4
推荐指数
1
解决办法
2794
查看次数

emacs的Saltstack模式

如何让Emacs使用Salt文件?是否有一些代码可用,或者我如何自己创建模式?

有一个Sublime Text插件可以做类似的事情.

谢谢

python emacs salt-stack

3
推荐指数
1
解决办法
857
查看次数

Python SyntaxError:"elif len(org)> = 1的语法无效:"

将以下函数导入Ipython 1.1解释器时,会引发SyntaxError,告诉我"elif len(org)> = 1:"语法无效.我的直接想法是我的间距应该是不正确的,但事实并非如此.

如果我们从第3行的初始for循环遍历此函数,则会在第三组if/elif语句上引发错误.

鉴于这是一个相当单一的功能,最好将其分解为几个较小的功能.那应该解决SyntaxError.

您是否同意将此功能分解为较小的功能是解决此错误的最佳方法?你知道为什么在间距和语法出现时出现错误,至少在我看来是正确的吗?

感谢您的时间!

def get_it(test_this):
    find_full_word = []
    for token in st.tag(test_this.split()):
        name = []
        org = []
        loc = []
        if len(token) == 2:
            x,y = token
            if y == 'PERSON':  
                name.append(x)
            elif y == 'ORGANIZATION':
                org.append(x)
            elif y == 'LOCATION':
                loc.append(x)
            elif y == 'O':
                if len(name) >= 1:
                    n_term = ""
                    for n_item in name:
                        if len(n_term) > 0:
                            n_term = n_term + " " + n_item
                        else:
                            n_term = n_item …
Run Code Online (Sandbox Code Playgroud)

python syntax-error

1
推荐指数
1
解决办法
2534
查看次数

为什么负数通常需要括号来获得平方时的预期结果?

有人知道为什么负数需要括号的方式可以像人们在几种编程语言中所期望的那样,而不是在C语言中(或者在其他语言中)?

这是R中的一个例子:

> -5^2
[1] -25
> # now let's use parentheses
> (-5)^2
[1] 25  
Run Code Online (Sandbox Code Playgroud)

Python中也发生了同样的事情:

>>> -5**2
-25
>>> (-5)**2
25
Run Code Online (Sandbox Code Playgroud)

只是为了好玩,我们也可以在Ruby中尝试这个(使用http://repl.it/作为解释器):

 > 5**2
=> 25
 > -5**2
=> -25
 > (-5)**2
=> 25
Run Code Online (Sandbox Code Playgroud)

但是,如果我们在C中实现这个短程序,那么负数不需要括号正确平方:

#include <stdio.h>

int main(void){
  int number, product;
  printf("\nEnter the number you want squared: ");
  scanf("%d", &number);
  product = number * number;
  printf("Squared number: %d \n", product);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是C程序的输出:

Enter the number you want squared: 5
Squared number: 25 
Run Code Online (Sandbox Code Playgroud)

接下来,我将使用负数: …

c ruby python r operator-precedence

-3
推荐指数
1
解决办法
501
查看次数