0

这允许我在我的计算机上使用 python 连接到一个网站:

from twill.commands import go, show, showforms, formclear, fv, submit

from bs4 import BeautifulSoup as bs

go('http://www.pge.com')
showforms()

这让我在谷歌应用引擎上打招呼,斜纹和美丽的汤导入工作:

import webapp2
import sys
sys.path.insert(0, 'libs')
from twill.commands import go, show, showforms, formclear, fv, submit
from bs4 import BeautifulSoup as bs

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!  I love dog food.')

application = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)

现在在此之后我尝试使用斜纹连接到一个网站并失败:

我在哪里可以调用 go() 连接到网站?

如果我在class MainPage(webapp2.RequestHandler):它挂起之前添加它并且我没有进入你好世界。

如果我将它添加到第一行的 MainPage 类中getit = go('http://www.pge.com'),或者只是go('http://www.pge.com'),它也会挂起,我不会进入 hello world。

如果我在里面添加它def: get(self):,我会得到:

内部服务器错误

服务器出错或无法执行请求的操作。还有一堆关于twill和mechanize.py的东西,然后是

File "..../twill/utils.py", line 275, in run_tidy process = subprocess.Popen(_tidy_cmd, stdin=subprocess.PIPE, AttributeError: 'module' object has no attribute 'Popen'

我是否以某种方式缺少其他一些依赖项,例如 mechanize.py?还是我需要做其他事情?

4

1 回答 1

0

这有助于部分解决我的问题,但后来出现了其他问题,特别是在填写表格、提交表格以及在网站中进一步迁移方面。

import webapp2
import sys
sys.path.insert(0, 'libs')
from twill.commands import go, show, showforms, formclear, fv, submit, config
from twill.browser import *
from bs4 import BeautifulSoup as bs


class MainPage(webapp2.RequestHandler):
    config('use_tidy', '0')
    def get(self):
        go('http://www.pge.com')

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!  I love dog food.')
        self.response.write(show())

tidy 以这种方式被禁用(tidy 不能很好地与谷歌沙箱配合使用),我可以通过self.response.write(show()).

附带说明:在使用 Google 应用引擎时,请小心使用 TextEdit 编辑 .py 文件。我收到了各种奇怪的非 ascii 字符错误。我添加了 # - - coding: utf-8 - - 到 python 文件的第一行,这有点帮助,然后切换到使用 pycharm 的 ide,这确实有帮助。

斜纹在谷歌应用引擎的沙箱中给了我各种各样的问题,我没有进入我的系统。我终于可以得到一个网页的 html,但不能像我在我的系统上那样简单地提交表单。showforms() 甚至没有显示表单,可能是因为我禁用了 tidy 并且 html 没有被正确解析?

我认为在这里向前迈进的一种方法是整理斜纹布里面的工作。显然,障碍正在“幕后”受到打击,我很难看到它们。

看起来斜纹布是一种高级抽象,现在在谷歌应用引擎上使用可能不是一个好主意。接下来我会尝试切换到 mechanize.py,或者寻找另一个沙箱,也许是亚马逊?

于 2014-09-12T04:03:05.213 回答