小编Dan*_*Bak的帖子

由于超时,pipenv 安装失败

C:\Users\danie01\fox\test\robotframework>pipenv install --python 3.5
Creating a virtualenv for this project...
Pipfile: C:\Users\danie01\fox\test\robotframework\Pipfile
Using C:\Users\danie01\AppData\Local\Programs\Python\Python35\python.exe (3.5.0) to create virtualenv...
Traceback (most recent call last):
  File "c:\users\danie01\appdata\local\programs\python\python36-32\lib\site-packages\pipenv\vendor\pexpect\expect.py", line 109, in expect_loop
    return self.timeout()
  File "c:\users\danie01\appdata\local\programs\python\python36-32\lib\site-packages\pipenv\vendor\pexpect\expect.py", line 82, in timeout
    raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: <pexpect.popen_spawn.PopenSpawn object at 0x05020630>
searcher: searcher_re:
    0: EOF

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\danie01\appdata\local\programs\python\python36-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\danie01\appdata\local\programs\python\python36-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\danie01\AppData\Local\Programs\Python\Python36-32\Scripts\pipenv.exe\__main__.py", …
Run Code Online (Sandbox Code Playgroud)

python installation virtualenv pipenv

16
推荐指数
3
解决办法
8863
查看次数

Python - 使用Python 3 urllib发出POST请求

我正在尝试向以下页面发出POST请求:http://search.cpsa.ca/PhysicianSearch

为了模拟单击"搜索"按钮而不填写任何表单,这会向页面添加数据.在查看chrome开发人员工具中的网络选项卡时,通过单击按钮获取POST标头信息.我发布这个而不是仅仅从其他类似问题复制解决方案的原因是我相信我可能没有得到正确的标题信息.格式是否正确,我是否抓住了正确的信息?我之前从未发过过POST请求.

这就是我设法拼凑的东西:

import urllib.parse
import urllib.request


data = urllib.parse.urlencode({'Host': 'search.cpsa.ca', 'Connection': 'keep-alive', 'Content-Length': 23796,
                                     'Origin': 'http://search.cpsa.ca', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                                     'Cahce-Control': 'no-cache', 'X-Requested-With': 'XMLHttpRequest',
                                     'X-MicrosoftAjax': 'Delta=true', 'Accept': '*/*',
                                     'Referer': 'http://search.cpsa.ca/PhysicianSearch',
                                     'Accept-Encoding': 'gzip, deflate',
                                     'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6',
                                     'Cookie': 'ASP.NET_SessionId=kcwsgio3dchqjmyjtwue402c; _ga=GA1.2.412607756.1459536682; _gat=1'})


url = "http://www.musi-cal.com/cgi-bin/query?%s"

data = data.encode('ascii')
with urllib.request.urlopen("http://search.cpsa.ca/PhysicianSearch", data) as f:
    print(f.read().decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)

此解决方案输出页面的HTML,但不包含我想要从POST请求中检索的任何数据.

python post google-chrome http web-scraping

15
推荐指数
4
解决办法
4万
查看次数

AssertionError:Egg-link ..与ReviewBoard的安装位置不匹配(at/...)

AssertionError: Egg-link /home/daniel/other-rb/reviewboard does not match installed location of ReviewBoard (at /home/daniel/reviewboard)
Run Code Online (Sandbox Code Playgroud)

我相信这是因为我使用相同的virtualenv在其他位置安装包.

我该如何解决这个问题?我猜我需要重定向Egg-link以匹配安装位置?

堆栈跟踪:

(reviewboard-3.0) daniel@beepboop ~/reviewboard $ ./setup.py develop
/home/daniel/envs/reviewboard-3.0/local/lib/python2.7/site-packages/setuptools/dist.py:334: UserWarning: Normalizing '4.0alpha0' to '4.0a0'
  normalized_version,
running develop
Requirement already up-to-date: setuptools in /home/daniel/envs/reviewboard-3.0/lib/python2.7/site-packages
Requirement already up-to-date: appdirs>=1.4.0 in /home/daniel/envs/reviewboard-3.0/lib/python2.7/site-packages (from setuptools)
Requirement already up-to-date: packaging>=16.8 in /home/daniel/envs/reviewboard-3.0/lib/python2.7/site-packages (from setuptools)
Requirement already up-to-date: six>=1.6.0 in /usr/lib/python2.7/dist-packages (from setuptools)
Requirement already up-to-date: pyparsing in /home/daniel/envs/reviewboard-3.0/lib/python2.7/site-packages (from packaging>=16.8->setuptools)
Requirement already up-to-date: pip in /home/daniel/envs/reviewboard-3.0/lib/python2.7/site-packages
Obtaining file:///home/daniel/reviewboard
Requirement already satisfied: django-haystack<=2.4.999,>=2.3.1 …
Run Code Online (Sandbox Code Playgroud)

python egg pip virtualenv

15
推荐指数
3
解决办法
5269
查看次数

python unittest:模拟一个类似字典的对象

我正在尝试执行以下操作:

def test_fn(self):
    cool_dict = {}
    blah = Mock(spec=DictField, wraps=cool_dict)
    blah['key'] = 'val'
    print(cool_dict))
    return False
Run Code Online (Sandbox Code Playgroud)

基本上,我想确保blaha 允许发生的任何事情DictField,但我希望发生的任何事情blah实际发生在cool_dict,因此我可以看到断言它具有某种状态。

我怎样才能做到这一点?上面的代码失败:

FAILED (errors=1)

Error
Traceback (most recent call last):
  File "C:\Program Files\Python 3.5\Lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Program Files\Python 3.5\Lib\unittest\case.py", line 605, in run
    testMethod()
  File "C:\Users\danie01.AD\PycharmProjects\component\component\Default\Functional\pam_team_management\test\test_team_create.py", line 23, in test_populate_info_page
    blah['key'] = 'val'
TypeError: 'Mock' object does not support item assignment
Run Code Online (Sandbox Code Playgroud)

我也尝试过MagicMock

def test_populate_info_page(self):
    cool_dict = {}
    blah = …
Run Code Online (Sandbox Code Playgroud)

python python-unittest

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

获取目录中每个文件的最后修改时间

我正在尝试获取特定目录中每个文件的数据。现在我只是想获得最后修改日期。似乎我需要将此 WindowsPath 转换为字符串,但我找不到任何可以执行此操作的函数。

import os
import time
from pathlib import Path

startDir = os.getcwd()

pt = r"\\folder1\folder2"

asm_pths = [pth for pth in Path(pt).iterdir()
            if pth.suffix == '.xml']


for file in asm_pths:
    (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
    print("last modified: %s" % time.ctime(mtime))
Run Code Online (Sandbox Code Playgroud)

安慰:

Traceback (most recent call last):
  File "C:\Users\daniel.bak\My Documents\LiClipse Workspace\Crystal Report Batch Analyzer\Analyzer\analyzer.py", line 34, in <module>
    (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
TypeError: argument …
Run Code Online (Sandbox Code Playgroud)

python

8
推荐指数
3
解决办法
5278
查看次数

CLion:运行目标后运行脚本

我想在运行目标可执行文件后运行一些命令。

我知道我可以添加“启动前”任务;有没有“启动后”任务之类的东西?

clion

7
推荐指数
0
解决办法
305
查看次数

在会话中发出后续POST请求不起作用 - Web抓取

这就是我要做的事情:去这里,然后点击"搜索".抓取数据,然后点击"下一步",然后继续按下,直到你没有页面.一切都达到了"下一步"的作用.这是我的代码.r.content的格式与我打印它的两次完全不同,表明GET和POST请求之间发生了不同的事情,即使我想要非常相似的行为.为什么会发生这种情况?

我觉得奇怪的是,即使在POST请求似乎返回错误的东西后,我仍然可以解析我需要的URL - 只是不是__EVENTVALIDATION输入字段.

错误消息(代码结束)表示内容不包含我需要发出后续请求的数据,但导航到页面显示它确实有这些数据,并且格式非常类似于第一页.

编辑:我正在打开基于HTML解析的网页,而且肯定是不对的.运行下面的代码将打开这些页面.

GET为我提供了一个包含以下数据的网站:

<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="4424DBE6">
<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="TlIgNH
Run Code Online (Sandbox Code Playgroud)

虽然POST以明文形式在页面底部生成一个包含所有数据的站点,如下所示:

|0|hiddenField|__EVENTTARGET||0|hiddenField|__EVENTARGUMENT||0|hiddenField|_
Run Code Online (Sandbox Code Playgroud)

坏r.content

好的r.content

import requests
from lxml import html
from bs4 import BeautifulSoup



page = requests.get('http://search.cpsa.ca/physiciansearch')
print('got page!')
d = {"ctl00$ctl13": "ctl00$ctl13|ctl00$MainContent$physicianSearchView$btnSearch",
     "ctl00$MainContent$physicianSearchView$txtLastName": "",
     'ctl00$MainContent$physicianSearchView$txtFirstName': "",
     'ctl00$MainContent$physicianSearchView$txtCity': "",
     "__VIEWSTATEENCRYPTED":"",
     'ctl00$MainContent$physicianSearchView$txtPostalCode': "",
     'ctl00$MainContent$physicianSearchView$rblPractice': "",
     'ctl00$MainContent$physicianSearchView$ddDiscipline': "",
     'ctl00$MainContent$physicianSearchView$rblGender': "",
     'ctl00$MainContent$physicianSearchView$txtPracticeInterests': "",
     'ctl00$MainContent$physicianSearchView$ddApprovals': "",
     'ctl00$MainContent$physicianSearchView$ddLanguage': "",
     "__EVENTTARGET": "ctl00$MainContent$physicianSearchView$btnSearch",
     "__EVENTARGUMENT": "",
     'ctl00$MainContent$physicianSearchView$hfPrefetchUrl': "http://service.cpsa.ca/OnlineService/OnlineService.svc/Services/GetAlbertaCities?name=",
     'ctl00$MainContent$physicianSearchView$hfRemoveUrl': "http://service.cpsa.ca/OnlineService/OnlineService.svc/Services/GetAlbertaCities?name=%QUERY",
     '__ASYNCPOST': 'true'}

h ={ …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup web-scraping python-requests

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

测试从标准输入读取和输出到标准输出的方法

我有一个方法从标准输入读取行并将行写入标准输出.

在JUnit测试中,如何将输入发送到方法,以及如何捕获其输出以便我可以对其进行断言?

java testing junit unit-testing

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

requirements.txt:以python版本为条件

我的requirements.txt已包含dataclasses在内,以向后移植python 3.6

然而,一些构建机器和同事使用 python 3.7,当尝试安装此模块时会失败。

有没有办法让数据类仅在必要时安装?

python dependency-management requirements.txt

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

在“for in”循环中访问迭代器

根据我的理解,当运行如下代码时:

for i in MyObject:
    print(i)
Run Code Online (Sandbox Code Playgroud)

MyObject 的__iter__函数已运行,for 循环使用它返回的迭代器来运行循环。

是否可以在循环中访问此迭代器对象?它是一个隐藏的局部变量,还是类似的东西?

我想做以下事情:

for i in MyObject:
    blah = forloopiterator()
    modify_blah(blah)
    print(i)
Run Code Online (Sandbox Code Playgroud)

我想这样做是因为我正在构建一个调试器,并且我需要在实例化迭代器后修改它(添加一个要在此循环期间、执行过程中迭代的对象)。我知道这是一种黑客行为,不应该以常规方式完成。直接修改 MyObject.items (迭代器正在迭代的内容)不起作用,因为迭代器仅计算一次。所以我需要直接修改迭代器。

python

3
推荐指数
2
解决办法
1287
查看次数